.net Core - 使用 Route 属性时未加载默认控制器 [英] .net Core - Default controller is not loading when Route attribute is used

查看:34
本文介绍了.net Core - 使用 Route 属性时未加载默认控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个新的.net core web应用项目带有如下路由配置:

A new .net core web application project comes with the following route configuration:

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });

如果您将其替换为 app.UseMvc() 并为 HomeController 及其操作(索引、关于、联系人、错误)添加适当的 Route 属性,它将仍然工作.由于我们没有指定默认路由,如果您点击 http://localhost 将不会呈现默认视图(主页/索引):25137/.希望理解正确!

If you replace this with app.UseMvc() and add appropriate Route attributes for both HomeController and its actions (Index, About, Contact, Error), it would still work. Since we're not specifying a default route, the default view (Home/Index) will not be rendered if you hit http://localhost:25137/. Hope that understanding is correct!

现在,因为我需要在点击 http://localhost:25137/ 时显示默认视图,我将路由代码更改为 app.UseMvcWithDefaultRoute(); 根据定义,它与初始代码段等效.即便如此,它也没有呈现默认视图;但在使用完整 URL 时有效(http://localhost:25137/home/index).这意味着路由仍然有效,但不是默认的!

Now, since I need the default view to be shown when the http://localhost:25137/ is hit, I changed the routing code to app.UseMvcWithDefaultRoute(); which by definition will do the equivalent to the initial snippet. Even then, it was not rendering the default view; but worked when used the complete URL(http://localhost:25137/home/index). That means the routing still works but not the default one!

然后我回到控制器并从 HomeController 及其操作中删除所有 Route 属性.然后默认路由解决了任何问题.

Then I went back to the controller and removed all the Route attribute from the HomeController and its actions. Then the default routing worked with out any issues.

这是预期的行为吗?这种行为背后的原因可能是什么?

Is that the expected behavior? What could be the reason behind this behavior?

推荐答案

来自 asp 文档:

动作要么按常规路由,要么按属性路由.放置控制器上的路由或动作使其属性路由.定义属性路由的操作无法通过常规路线,反之亦然.上的任何路由属性控制器使控制器属性中的所有动作都被路由.

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.

所以基本上如果你使用属性,那么在 UseMvcUseMvcWithDefaultRoute 中定义的路由将被忽略.在这种情况下,只会使用该属性.

So basically if you use attributes then the routes defined in UseMvc or UseMvcWithDefaultRoute will be ignored. Only the attribute would be used in that case.

如果您想获得与带有可选段的默认路由类似的效果,您仍然可以使用多个路由属性.再次来自 asp 文档 中的同一篇文章:

You could still use multiple route attributes if you wanted to achieve a similar effect than the default route with optional segments. Again from the same article in the asp docs:

public class MyDemoController : Controller
{
   [Route("")]
   [Route("Home")]
   [Route("Home/Index")]
   public IActionResult MyIndex()
   {
      return View("Index");
   }
   [Route("Home/About")]
   public IActionResult MyAbout()
   {
      return View("About");
   }
   [Route("Home/Contact")]
   public IActionResult MyContact()
   {
      return View("Contact");
   }
}

这篇关于.net Core - 使用 Route 属性时未加载默认控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆