【问题标题】:If I set default route and attribute routing to same controller in mvc5 then conventional routing is not working如果我在 mvc5 中将默认路由和属性路由设置为同一控制器,则常规路由不起作用
【发布时间】:2021-08-13 08:21:03
【问题描述】:

控制器

    [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"

【问题讨论】:

    标签: c# asp.net routes asp.net-mvc-5


    【解决方案1】:

    我对此进行了研究,并在 microsot docs 上找到了这个。

    动作要么是传统路由,要么是属性路由。配售 控制器上的路由或操作使其属性路由。 定义属性路由的操作无法通过 常规路线,反之亦然。上的任何路由属性 控制器使控制器属性中的所有动作都被路由。

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

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

    【讨论】:

      猜你喜欢
      • 2016-07-03
      • 1970-01-01
      • 2018-01-14
      • 2021-06-10
      • 2014-12-27
      • 2016-01-06
      • 2017-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多