【问题标题】:Changing URL without changing Actual Path to Redirect在不更改实际重定向路径的情况下更改 URL
【发布时间】:2013-07-03 07:14:04
【问题描述】:

我是 ASP.Net 的新手,正在使用 MVC 4。我想用自定义的 URL 替换我当前的 URL。

例如: 当前网址:http://www.testsite.com/home?pageId=1002

所需网址:http://www.testsite.com/1002/home/

因此,地址栏中显示的 URL 将是所需的 URL,而实际工作的 URL 将是当前的。

我已尝试在项目的 Global.asax 文件中进行 URL 路由,但似乎对我不起作用。

我真正想要的是把 URL 像这样。

提前致谢。

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-4


    【解决方案1】:

    ASP.NET MVC 4 提供了一种工具箱方式来编写您的应用程序。您在浏览器中看到的 URL 来自 Routing,它努力将 url 转换为应用程序路由和应用程序路由到 url。

    1) 默认的 ASP.NET MVC 4 模板项目在 App_Start 文件夹中带有一个名为 RouteConfig 的文件,您必须在其中配置应用程序的路由。

    2) 路由有优先顺序,因此,将此路由放在默认路由之前:

     routes.MapRoute(
                name: "RouteForPageId",
                url: "{pageId}/{action}",
                //controller = "Home" and action = "Index" are the default value,
                //change for the Controller and action that you have
                //pageId is the parameter from the action that will return the page
                defaults: new { controller = "Home", action = "Index" }
            );
    

    现在可以输入 myappdomain/1220/index 为例。

    希望对您有所帮助!在这里查看更多信息ASP.NET Routing

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-22
      • 2013-08-11
      • 1970-01-01
      • 2023-03-30
      • 2017-02-16
      • 1970-01-01
      • 1970-01-01
      • 2013-10-12
      相关资源
      最近更新 更多