Html.BeginForm 正在区域后自动添加子文件夹

Html.BeginForm is automatically adding a sub-folder after the area

我在我的 ASP.NET MVC 应用程序中创建了一个名为 B2b 的区域,我还在该区域下创建了一个名为 Shopify 的子文件夹:

为了注册 Shopify 子文件夹,我创建了一个 CustomViewEngine 如下 (followed this tutorial):

public class ExpandedViewEngine : RazorViewEngine 
{
    public ExpandedViewEngine()
    {
        var extendedViews = new[] 
        {
            "~/Areas/B2b/Views/Shopify/{1}/{0}.cshtml",
        };

        var extendedPartialViews = new[]
        {
            "~/Areas/B2b/Views/Shopify/Shared/{0}.cshtml"
        };

        ViewLocationFormats = ViewLocationFormats.Union(extendedViews).ToArray();
        PartialViewLocationFormats = PartialViewLocationFormats.Union(extendedPartialViews).ToArray();
    }
}

这是我的区域注册码(我正在使用lowercase-dashed-route):

public class B2bAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "B2b";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        // this route is for controllers and views inside Shopify sub-folder
        var shopifyDefaultRoute = new LowercaseDashedRoute(
            "B2b/Shopify/{controller}/{action}/{id}",
            new RouteValueDictionary(new { controller = "ProductMap", action = "Display", id = UrlParameter.Optional }),
            new DashedRouteHandler(),
            this,
            context,
            new[] { "Shopless.Web.Areas.B2b.Controllers.Shopify" }
        );
        context.Routes.Add("Shopify_default", shopifyDefaultRoute);

        // default area route which is not under Shopify subfolder
        var b2bDefaultRoute = new LowercaseDashedRoute(
            "B2b/{controller}/{action}/{id}",
            new RouteValueDictionary(new { action = "index", id = UrlParameter.Optional }),
            new DashedRouteHandler(),
            this,
            context,
            new[] { "Shopless.Web.Areas.B2b.Controllers" }
        );
        context.Routes.Add("B2b_default", b2bDefaultRoute);
    }
}

然后我在 Global.asax 中注册了上面的内容:

protected void Application_Start()
{

    ViewEngines.Engines.Add(new ExpandedViewEngine());
    AreaRegistration.RegisterAllAreas();
    // more code ...
}

一切正常,除了以下代码

@using (Html.BeginForm("update", "organisation", new { area = "B2b" }, FormMethod.Post))
{
    <input type="text" id="name" name="name">
}

正在生成以下 HTML:

<form action="/b2b/shopify/organisation/update" method="post" novalidate="novalidate">
    <input type="text" id="name" name="name">
</form>

注意在我的区名后面加了shopify B2b。上面的表格在 B2b 区域内,但不在 shopify 子文件夹下,所以不确定为什么要添加它?

它正在映射到此路由模板 "B2b/Shopify/{controller}/{action}/{id}",因为它也匹配在为表单生成 URL 时给予 BeginForm 的常规值。

URL 生成的两个区域路由约定相互冲突。

如果我要求路由 table 生成一个 URL 并且我给它一个 controller, action,以及面积。给定同一区域内的以下路线模板,哪条路线将首先匹配

  • B2b/Shopify/{controller}/{action}/{id}

  • B2b/{controller}/{action}/{id}

并且由于第一场比赛总是赢,它将映射到上面的第一场比赛,这将解释您的表格的当前体验。

如果您想使用特定路径为表单生成 URL,请使用 BeginRouteFormMethod

@using (Html.BeginRouteForm(
    routeName: "B2b_default", 
    routeValues: new { action = "update", controller = "organisation", area = "B2b" }, 
    method: FormMethod.Post)
)
{
    <input type="text" id="name" name="name">
}