我在 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}”
,因为它也与为BeginForm指定的常规值匹配
code> 生成表单的 URL 时。
两个区域路由约定在 URL 生成方面相互冲突。
如果我要求路由表生成一个 URL,并给它一个 Controller 、操作和区域。给定同一区域内的以下路线模板,哪条路线将首先匹配
B2b/Shopify/{controller}/{action}/{id}
B2b/{controller}/{action}/{id}
由于第一场比赛总是获胜,因此它将映射到上面的第一场比赛,这将解释您表单的当前体验。
如果您想使用特定路由来生成表单的 URL,请使用 BeginRouteForm
Method
@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/