如何从网址中删除".aspx"扩展名



如何在 c# 中从页面 URL 中删除扩展名。

例如:questions/ask.aspx

我希望我的 Web 应用程序的 url 采用以下格式:

questions/ask

如果有人有想法,那么请指导我...

如果您使用的是 Web 表单,则需要在Global.asax文件中使用 URL 路由添加自定义路由器处理程序。查看此示例:

全球

public class Global : System.Web.HttpApplication
{
    //Register your routes, match a custom URL with an .aspx file. 
    private void RegisterRoutes(RouteCollection routes)
    {
        routes.MapPageRoute("About", "about", "~/about.aspx");
        routes.MapPageRoute("Index", "index", "~/index.aspx");
    }
    //Init your new route table inside the App_Start event.
    protected void Application_Start(object sender, EventArgs e)
    {
        this.RegisterRoutes(RouteTable.Routes);
    } 
}   

你必须实现URL Rewriting

URL 重写是拦截传入 Web 请求的过程 并将请求重定向到其他资源。执行时 URL 重写,通常检查所请求的 URL,并且基于 在其值上,请求被重定向到其他 URL

您可以在Web.Config中添加此内容

<urlMappings enabled="true">
 <add url="~/questions/ask" mappedUrl="~/questions/ask.aspx?page=Ask"/>
</urlMappings>

看这里

最新更新