简体   繁体   English

ASP.NET Core 2.1中的重定向路由

[英]Redirect routing in ASP.NET Core 2.1

I have a main project with some controllers, eg HomeController containing an action "Index". 我有一个带有一些控制器的主项目,例如HomeController包含一个动作“ Index”。 This action is reached by www.mysite.com/home/index. 该操作可通过www.mysite.com/home/index进行。

Then I have another project called "plugin" , which is referenced within the main project. 然后,我有另一个名为“插件”的项目 ,该项目在主项目中被引用。 There is a controller, eg CustomerController . 有一个控制器,例如CustomerController An action within this controller contains a routing attribute "[Route("edit")]" . 该控制器内的动作包含路由属性“ [Route(” edit“)]”“ This action is reached by www.mysite.com/customer/edit . 该操作可通过www.mysite.com/customer/edit进行 But I want that this action can be reached by www.mysite.com/plugin/customer/edit containing the name of the project (or another name). 但是我希望可以通过包含项目名称(或其他名称)的www.mysite.com/plugin/customer/edit来执行此操作。

How can I do this without setting routing attribute for every controller in my "plugin" project? 如何在不为 “插件”项目中的每个控制器设置路由属性的情况下执行此操作?

Btw.. I'm using NopCommerce 4.1 if it's necessary to know.. 顺便说一句..如果有必要,我正在使用NopCommerce 4.1。

This is scenario for Areas. 这是区域的方案。

1) Inside your plugin create folder structure 1)在您的插件内部创建文件夹结构

Areas
..Plugin
....Controllers
....Views

2) Inside controllers create base plugin controller "PluginController" where you set Area attribute 2)在内部控制器中创建基础插件控制器“ PluginController”,在其中设置Area属性

[Area("Plugin")]
public class PluginController : Controller
{
    ...
}

3) Make all your plugin controllers inherit from PluginController 3)让您所有的插件控制器都继承自PluginController

public class CustomerController : PluginController
{
    ...
}

4) Add support for areas into route builder 4)在路线构建器中添加对区域的支持

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

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

Now all actions inside your plugin will require www.mysite.com/plugin/ ... 现在,您插件中的所有操作都将需要www.mysite.com/plugin/ ...

I'll also note that If you wish to retrieve action urls from outside the plugin you need to specify area of the controller like so: 我还要注意,如果您希望从插件外部检索操作网址,则需要指定控制器的区域,如下所示:

@Url.Action("Edit", "Customer", new { Area = "Plugin" })

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

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