简体   繁体   English

如何在我的ASP.NET MVC应用程序中设置此区域

[英]How to set this Area up in my ASP.NET MVC Application

I'm trying to setup an Area Route in my ASP.NET MVC application. 我正在尝试在ASP.NET MVC应用程序中设置区域路由。

I'm also using the nuget package AttributeRouting , not the normal MVC register area routes thingy. 我也在使用nuget包AttributeRouting ,而不是普通的MVC寄存器区域路由内容。

From my understanding, area routes look like this : /area/controller/method 据我了解,区域路线如下所示: /area/controller/method

What I'm trying to do is :- /api/search/index 我想做的是:- /api/search/index

which means: 意思是:

  • Area => Api 面积=> Api
  • Controller => SearchController 控制器=> SearchController
  • ActionMethod => Index ActionMethod =>索引

.

[RouteArea("Api")]
public class SearchController : Controller
{
    [POST("Index")]
    public JsonResult Index(IndexInputModel indexInputModel) { .. }
}

But that doesn't create that route. 但这并没有创造出这种路线。 This is what it creates: /api/index The search controller is missing. 它是由它创建的: /api/index缺少search控制器。

I've had a look the docs and noticed the RoutePrefix so I tried this.. 看了看文档并注意到RoutePrefix所以我尝试了这个。

[RouteArea("Api")]
[RoutePrefix("Search")]
public class SearchController : Controller
{
    [POST("Index")]
    public JsonResult Index(IndexInputModel indexInputModel) { .. }
}

and that actually creates the route /api/search/index . 并且实际上创建了路由/api/search/index

But why do i need to put the RoutePrefix in there? 但是为什么我需要在其中放置RoutePrefix Shouldn't it be smart enough to already figure out that this is a SearchController and create the 3-segment route? 是否已经足够聪明,已经弄清楚这是一个SearchController并创建了3段路线?

You don't need to put a RoutePrefix anywhere. 您无需在任何地方放置RoutePrefix。 It's just there as a refactoring/DRY aid. 它只是作为重构/干燥辅助工具而存在的。 Consider: 考虑:

[RouteArea("Api")]
public class SearchController : Controller 
{
    [POST("Search/Index")]
    public ActionResult Index() { }
}

If you had a number of actions, maybe you want them all with the "Search" prefix, so you'd do: 如果您有许多操作,则可能希望所有操作都带有“ Search”前缀,因此您可以执行以下操作:

[RouteArea("Api")]
[RoutePrefix("Search")]
public class SearchController : Controller 
{
    [POST("Index")]
    public ActionResult Index() { }

    // Other actions to prefix....
}

Shouldn't it be smart enough? 它不够聪明吗?

Not to be cheeky, but no. 不要厚脸皮,但不要。 AR was never intended to read all your code for you and magically generate routes. AR从未打算为您阅读所有代码并神奇地生成路由。 It was intended to keep your URLs top of mind, and to do that you should see your URLs. 这样做是为了使您的URL成为首要考虑因素,并且应该看到 URL。 Not that this is the best or only way of doing things, just that was my intent from the get. 并不是说这是最好的或唯一的做事方式,但这只是我一开始的意图。

The real reason why it isn't smart enough is that the concept of "Area" has nothing to do with URL. 它不够智能的真正原因是“区域”的概念与URL无关。 An area is a logical unit. 区域是逻辑单元。 You could expose that logical unit without any route prefix (so it would be hanging off ~/) or you could expose it off "This/Is/A/Prefix". 您可以公开该逻辑单元而没有任何路由前缀(因此它将挂起〜/),也可以公开其“ This / Is / A / Prefix”。

However, if you want it to be smart enough.... I just released v3.4, which will let you do this (if you want to; don't have to): 但是,如果您希望它足够聪明……...我刚刚发布了v3.4,它将允许您执行此操作(如果您愿意;不必这样做):

namespace Krome.Web.Areas.Api
{
    [RouteArea]
    [RoutePrefix]
    public class SearchController : Controller 
    {
        [POST]
        public ActionResult Index() { }
    }
}

This will yield the following route: ~/Api/Search/Index. 这将产生以下路线:〜/ Api / Search / Index。 The area comes from the last section of the controller's namespace; 该区域来自控制器名称空间的最后一部分; the route prefix comes from the controller name; 路由前缀来自控制器名称; and the rest of the url comes from the action name. 而其余网址则来自操作名称。

One more thing 还有一件事

If you want to get out a route area url and route prefix rat's nest for individual actions in a controller, do this: 如果要为控制器中的各个操作获取路由区域网址和路由前缀“鼠巢”,请执行以下操作:

[RouteArea("Api")]
[RoutePrefix("Search")]
public class SearchController : Controller 
{
    [POST("Index")]
    public ActionResult Index() { }

    [GET("Something")] // yields ~/Api/Search/Something
    [GET("NoPrefix", IgnoreRoutePrefix = true)] // yields ~/Api/NoPrefix
    [GET("NoAreaUrl", IgnoreAreaUrl = true)] // yields ~/Search/NoAreaUrl
    [GET("Absolutely-Pure", IsAbsoluteUrl = true)] // yields ~/Absolutely-Pure 
    public ActionResult Something() {}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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