【问题标题】:Redirect routing in ASP.NET Core 2.1ASP.NET Core 2.1 中的重定向路由
【发布时间】:2018-07-13 13:36:05
【问题描述】:

我有一个带有一些控制器的主项目,例如HomeController 包含一个动作“索引”。此操作由 www.mysite.com/home/index 完成。

然后我有另一个名为“插件”的项目,它在主项目中被引用。 有一个控制器,例如客户控制器。此控制器中的操作包含 路由属性“[Route("edit")]"。 此操作由 www.mysite.com/customer/edit 完成。但我希望包含项目名称(或其他名称)的 www.mysite.com/plugin/customer/edit 可以执行此操作。

如何在我的“插件”项目中为每个控制器设置路由属性

顺便说一句..如果有必要知道的话,我正在使用 NopCommerce 4.1..

【问题讨论】:

标签: c# nopcommerce asp.net-core-2.1 .net-core-2.1


【解决方案1】:

这是区域的场景。

1) 在插件内部创建文件夹结构

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

2) 在控制器内部创建基本插件控制器“PluginController”,您可以在其中设置区域属性

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

3) 让你所有的插件控制器都继承自PluginController

public class CustomerController : PluginController
{
    ...
}

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?}");
});

现在,您插件中的所有操作都需要 www.mysite.com/plugin/ ...

我还要注意,如果您希望从插件外部检索操作 url,您需要指定控制器的区域,如下所示:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-04
    • 1970-01-01
    • 2020-07-24
    • 2019-01-13
    • 2019-03-05
    • 2013-05-12
    • 1970-01-01
    相关资源
    最近更新 更多