简体   繁体   中英

How to have specific routing in asp.net mvc 4

I would like to know how to have specific adrressing in MVC.net 4 I have 3 url types in my web application 1-www,site.com/rss.xml 2-www.site.com/amir (this type of addresses are reserved for my users and i want to put my site's usernames in order to create a profile address for each of my users) 3-www.site.com/article/detail/1

And my RouteConfig file is like below

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" }
   );

but the problem is that all these 3 url types are not accessible, and only the 2nd works i would be happy if anyone can help me in this regard

Have you even looked at MSDN as they have a perfectly reasonable answer that make sense here: https://msdn.microsoft.com/en-us/library/cc668201.aspx#Anchor_2

All i did to get that result was google "ASP.net MVC 4 routing" and it was the 3rd result...

protected void Application_Start(object sender, EventArgs e)
{
    RegisterRoutes(RouteTable.Routes);
}

public static void RegisterRoutes(RouteCollection routes)
{
    routes.MapPageRoute("",
        "Category/{action}/{categoryName}",
        "~/categoriespage.aspx");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM