c# - Html.BeginForm 会在该区域后自动添加一个子文件夹

标签 c# asp.net-mvc asp.net-mvc-5 asp.net-mvc-routing asp.net-mvc-areas

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

enter image description here

为了注册 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}”,因为它也与为BeginForm指定的常规值匹配 code> 生成表单的 URL 时。

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

如果我要求路由表生成一个 URL,并给它一个 Controller 操作区域。给定同一区域内的以下路线模板,哪条路线将首先匹配

  • 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">
}

关于c# - Html.BeginForm 会在该区域后自动添加一个子文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60310852/

相关文章:

c# - 使用 EF Core 将 SQL 转换为 Linq

c# - 检查 pdb 文件是否与源匹配

javascript - requirejs 定义未定义

javascript - asp.net mvc ModelState IsValid - 有效处理 false 的返回

c# - 将 ViewModel 从 HttpGet 操作传递到 HttpPost 操作

c# - 好的编码? (多个消息循环)

c# - 重用文件流

c# - 无法在 C# MVC 中返回具有不同参数的 View() - Ajax 问题更新页面内容

c# - 如何使用 Bootstrap 添加事件样式

c# - Entity Framework 种子不工作