Html.BeginForm将在该区域后自动添加一个子文件夹

问题描述 投票:-1回答:1

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

enter image description here

为了注册Shopify子文件夹,我创建了一个如下的CustomViewEnginefollowed 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子文件夹下,因此不确定为什么要添加它吗?

c# asp.net-mvc asp.net-mvc-5 asp.net-mvc-routing asp.net-mvc-areas
1个回答
1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.