How to remove controller name from URL in MVC project

前端 未结 4 1548
不思量自难忘°
不思量自难忘° 2020-12-12 05:43

I have a URL like below and I need to remove the controller name (myController). I\'ve use several fixes but none fixed the issue. Please help me guys..

http://fold

相关标签:
4条回答
  • 2020-12-12 06:04

    You could try inverting the routes definition by placing the more specialized route first. Also you probably didn't want to hardcode the action name as action but rather use the {action} placeholder:

    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            //routes.MapMvcAttributeRoutes();
    
        routes.MapRoute(
            name: "Special",
            url: "{action}",
            defaults: new { controller = "Home", action = "LandingIndex" }
        );
    
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
       }
    }
    

    Follow the below links :

    Link 1

    Link 2

    Link 3

    0 讨论(0)
  • 2020-12-12 06:05

    Enable attribute routing by adding following line before route.MapRoute in RouteConfig.cs file

    routes.MapMvcAttributeRoutes();
    

    Then add the Route Attribute to each action with the route name, like:

    [Route("MyAction")]
    public ActionResult MyAction()
    {
    ...
    }
    
    0 讨论(0)
  • 2020-12-12 06:08

    You should map new route in the global.asax (add it before the default one), for example:

    routes.MapRoute("SpecificRoute", "{action}/{id}", new {controller = "MyController", action = "Index", id = UrlParameter.Optional});
    
    routes.MapRoute("Default", "{controller}/{action}/{id}", new {controller = "Home", action = "Index", id = UrlParameter.Optional} );
    
    0 讨论(0)
  • 2020-12-12 06:10

    Use Routing in the global.asax

    routes.MapRoute("MyAction", "{action}/{id}", new {controller = "MyController", action = "Index", id = UrlParameter.Optional});
    

    and in controller

    [Route("MyAction")]
    public ActionResult MyAction()
    {
     ...
     }
    

    add following to RouteConfig.cs

    routes.MapMvcAttributeRoutes();
    
    0 讨论(0)
提交回复
热议问题