【问题标题】:How to have specific routing in asp.net mvc 4如何在 asp.net mvc 4 中有特定的路由
【发布时间】:2016-03-05 07:46:25
【问题描述】:

我想知道如何在 MVC.net 4 中有特定的地址 我的 Web 应用程序中有 3 种 url 类型 1-www,site.com/rss.xml 2-www.site.com/amir(这种类型的地址是为我的用户保留的,我想输入我网站的用户名,以便为我的每个用户创建个人资料地址) 3-www.site.com/article/detail/1

我的 RouteConfig 文件如下所示

routes.MapRoute(
          name: "RSS_route", // Route name
          url: "rss.xml", // URL with parameters
          defaults: new { controller = "Common", action = "RSS", area = "" } // Parameter defaults
           , namespaces: new[] { "Clinic.WebUI.Controllers" }
     );

 routes.MapRoute(
     name: "Doctor",
     url: "{docname}",
     defaults: new { controller = "Doctor", action = "Doctor", docname = "test" }
  , namespaces: new[] { "Clinic.WebUI.Controllers" }
 );

 routes.MapRoute(
     name: "Default",
     url: "{controller}/{action}/{id}",
     defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    , namespaces: new[] { "Clinic.WebUI.Controllers" }
 );

 routes.MapRoute(
       name: "Article",
       url: "{controller}/{action}/{id}/{subject}",
       defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, subject = UrlParameter.Optional }
      , namespaces: new[] { "Clinic.WebUI.Controllers" }
   );

但问题是所有这 3 种 url 类型都无法访问,只有第 2 种有效 如果有人可以在这方面帮助我,我会很高兴

【问题讨论】:

    标签: c# asp.net asp.net-mvc asp.net-mvc-4 asp.net-mvc-routing


    【解决方案1】:

    你有没有看过 MSDN,因为他们有一个完全合理的答案,在这里很有意义:https://msdn.microsoft.com/en-us/library/cc668201.aspx#Anchor_2

    为了得到这个结果,我所做的只是谷歌“ASP.net MVC 4 路由”,这是第三个结果......

    protected void Application_Start(object sender, EventArgs e)
    {
        RegisterRoutes(RouteTable.Routes);
    }
    
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.MapPageRoute("",
            "Category/{action}/{categoryName}",
            "~/categoriespage.aspx");
    }
    

    【讨论】:

    • 感谢您的所有努力,但我认为您只是阅读了标题,总而言之,我已经阅读了您的所有参考,但我的问题仍然存在,正如我上面提到的我想让所有这 3 个 url 类型匹配。
    • 我的意思是不要编辑 RouteConfig 并在应用程序代码中执行它,看看是否有效?
    猜你喜欢
    • 2015-10-25
    • 1970-01-01
    • 2015-06-22
    • 1970-01-01
    • 1970-01-01
    • 2017-03-25
    • 2014-05-25
    • 1970-01-01
    • 2015-07-18
    相关资源
    最近更新 更多