如果我在 mvc5 中将默认路由和属性路由设置为同一控制器,则传统路由不起作用

If I set default route and attribute routing to same controller in mvc5 then conventional routing is not working

控制器

    [HttpGet]
[Route("find-a-doctor")]
public ActionResult FindADoctor()
{
    ViewData["sList"] = specialities ;
    return View();
}

Route.Config

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapMvcAttributeRoutes();
    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}",
        defaults: new { controller = "UserPanelFinADoctor", action = "FindADoctor" },
         namespaces: new string[] { "xyz.Controllers" }
    );
}

This is the result when I debug code. It says "The resource cannot be found"

我重新搜索了一下,在 microsot 文档上找到了这个。

Actions are either conventionally routed or attribute routed. Placing a route on the controller or the action makes it attribute routed. Actions that define attribute routes cannot be reached through the conventional routes and vice-versa. Any route attribute on the controller makes all actions in the controller attribute routed.

所以在你的例子中。因为你已经在基于属性的路由下注册了你的动作的路由。约定路由不起作用。 应该使用其中一个,不能同时使用。

Link 到文章 - https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-5.0#mixed-routing-attribute-routing-vs-conventional-routing