简体   繁体   English

如何将请求动态路由到特定的 controller 并使用原始请求路径作为参数进行操作

[英]How to dynamically route request to a specific controller and action with the original request paths as parameters

I am unable to find any documentation/details on the format of the middleware endpoints pattern field.我找不到有关中间件端点模式字段格式的任何文档/详细信息。 Or endpoints at all really.或者根本就没有端点。 Only examples of common use case scenarios for routing.仅用于路由的常见用例场景示例。

What I'd like to do is accept a request like:我想做的是接受这样的请求:

example.com/anything1/anything2

and send it forward to:并将其转发至:

example.com/Home/Display?var1=anything1&var2=anything2

The browser should display the original request and not change.浏览器应该显示原始请求并且不会改变。

My end goal is to return the home controller's layout with the view getting data from a database.我的最终目标是返回home控制器的布局, view从数据库中获取数据。

I've been throwing random data at it to see what breaks it but not successful at understanding what works.我一直在向它扔随机数据,看看是什么破坏了它,但没有成功理解什么是有效的。

pattern: "{custompath}/{custompage}");

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");
    endpoints.MapControllerRoute(
        name: "notDefault",
        pattern: "{area:exists}/{controller=Content}/{action=Index}/{id?}");
});

This is the current default setup that works with my areas and controllers but I don't fully understand why either.这是当前适用于我的区域和控制器的默认设置,但我也不完全理解为什么。

Update The migration article from asp.net core 2.2 to 3 says that "Endpoint Routing supports the same route pattern syntax and route pattern authoring features as IRouter".更新从 asp.net 核心 2.2 到 3 的迁移文章说“端点路由支持与 IRouter 相同的路由模式语法和路由模式创作功能”。 https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.0&tabs=visual-studio#mvc-service-registration https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.0&tabs=visual-studio#mvc-service-registration

There are no links to the expected syntax but I've found some old articles that have information.没有指向预期语法的链接,但我发现了一些包含信息的旧文章。 While being helpful they are still incomplete and/or inaccurate.虽然有帮助,但它们仍然不完整和/或不准确。 When applying the logic I have found bugs.应用逻辑时,我发现了错误。 This was the most informative.这是信息最丰富的。 https://aspnetcore.readthedocs.io/en/stable/fundamentals/routing.html https://aspnetcore.readthedocs.io/en/stable/fundamentals/routing.html

Some routes work but break when I add others.有些路线有效,但当我添加其他路线时会中断。 For instance, this route based on the article above should only be accepted when someone types "Account" as the first parameter but instead when requesting with no paths or arguments it manipulates the request to include Account the next route's controller default.例如,仅当有人键入“Account”作为第一个参数时,才应接受基于上述文章的此路由,但当请求没有路径或 arguments 时,它会操纵请求以包含 Account 下一个路由的 controller 默认值。 But then returns the last route as expected.但随后按预期返回最后一条路线。

When requesting https://localhost:port请求时https://localhost:port

endpoints.MapControllerRoute(
    name: "account",
    pattern: "Account/{controller=Dashboard}/{action=Overview}",
    defaults: new { area = "Account" });
endpoints.MapControllerRoute(
    name: "manage",
    pattern: "Manage/{controller=Content}/{action=Index}",
    defaults: new { area = "Manage" });
endpoints.MapControllerRoute(
    name: "default",
    pattern: "",
    defaults: new { controller = "Content", action = "Display", path = "Public", page = "Index" });

it manipulates the request to https://localhost:port/Account/Content/ and with contents from https://localhost:port/Content/Display/它处理对https://localhost:port/Account/Content/的请求以及来自https://localhost:port/Content/Display/的内容

I was expecting that when I didn't type in Account or Manage it would fall through to the blank route and stop there.我期待当我没有输入 Account 或 Manage 时,它会落入空白路线并停在那里。 But it seems that after it gets there it does some other stuff.但似乎在它到达那里之后它会做一些其他的事情。 Half of what I want is accomplished lol.我想要的一半已经完成了,哈哈。

Latest Update Currently, I have this working with minimal bugs.最新更新目前,我有这个工作与最小的错误。 Conceptually when making these I imagined that the pattern was the structure that exists in my app (controller > action > param, which is backward from what I originally imagined. (that the pattern was what the user request is*)从概念上讲,在制作这些时,我认为模式是我的应用程序中存在的结构(控制器 > 操作 > 参数,这与我最初想象的相反。(模式是用户请求的内容*)

    endpoints.MapControllerRoute(
                    name: "Public",
                    pattern: "{page}",
                    defaults: new { controller = "Content", action = "Display", path = "Public" });
                endpoints.MapControllerRoute(
                    name: "Default",
                    pattern: "{path=Public}/{page=Index}",
                    defaults: new { controller = "Content", action = "Display" });                
                endpoints.MapControllerRoute(
                    name: "Controllers",
                    pattern: "{controller}/{action}/{id?}");

notes:笔记:

"areaName:" adds the name in quotes to the request. "areaName:" 在请求中添加引号中的名称。 "defaults:" manipulates the request. "defaults:" 操纵请求。 I was able to inject a controller and action.我能够注入 controller 并采取行动。

The other thing to note is that these endpoints affect the generated routes for links that use asp-*另一件需要注意的是,这些端点会影响为使用 asp-* 的链接生成的路由

I don't think a document exists for the pattern syntax or format but I was able to get something working.我不认为模式语法或格式的文档存在,但我能够得到一些工作。

With the below code I was able to send a request like:使用以下代码,我能够发送如下请求:

example.com/anything1/anything2

and it was forwarded to the controller "Home", action "Display" with anything1 and anything2 sent as arguments.它被转发到 controller “Home”,动作“Display”,anything1 和anything2 作为 arguments 发送。 And the user still sees the original request.并且用户仍然看到原始请求。

app.UseEndpoints(endpoints =>
{
     endpoints.MapControllerRoute(
           name: "default",
           pattern: "{path}/{page}",
           defaults: new {controller="Home", action="Display", path="", page="" }); 
});

Additionally, adding text in the quotes of path and page will allow for default values to be sent if nothing is requested.此外,在路径和页面的引号中添加文本将允许在未请求任何内容时发送默认值。 I originally wrote middleware to handle writing the entire response but had to generate razor pages which ran much slower.我最初编写中间件来处理写入整个响应,但必须生成运行速度慢得多的 razor 页面。 This allows me to use the shared layouts which is what I wanted to start.这使我可以使用我想要开始的共享布局。

Note: The .net-core documentation on routing namespaces is limited to common use cases and method signatures which is rather useless because the default site templates start with that code.注意:关于路由命名空间的 .net-core 文档仅限于常见的用例和方法签名,因为默认站点模板以该代码开头,所以相当无用。 An old article for mvc routing is what made me think to try the code above but still have no idea what's going on in the background.一篇关于 mvc 路由的旧文章让我想到尝试上面的代码,但仍然不知道后台发生了什么。

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

相关问题 如何将请求路由到MVC2中特定控制器中的特定操作? - How do I route a request to a specific action in a specific controller in MVC2? 如何将特定的子域请求重新路由到其他ASP.NET MVC控制器? - How can I re-route a specific subdomain request to a different ASP.NET MVC controller? 如何将请求发送到www.MyDomain.com到特定的控制器类和方法 - How to route request to www.MyDomain.com to specific controller class and method 如何使用“controller?action¶meters”路由实现 .NET WebAPI? - How to implement a .NET WebAPI with a route of "controller?action&parameters"? 如何在.net转换/绑定之前获取请求原始参数? - How to get the request original parameters before the .net convertion / binding? 向控制器发送了后请求@action - sent post request @action to controller 在与该请求匹配的控制器上未找到任何操作 - No action was found on the controller that matches the request 在与该请求匹配的控制器上未找到任何操作 - No action was found on the controller that matches the request 如何设置Web API控制器以将请求路由到方法参数? - How to setup Web API controller to route request to method parameter? 创建一条路径以接受特定控制器的两个参数 - Creating a route to accept two parameters for specific controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM