【发布时间】:2023-04-03 01:25:01
【问题描述】:
我有一个网站,我们最近对它进行了一些更改,这似乎破坏了我的一些路由。
最初的问题是我在使用以下表格时收到了 405 动词不允许。
using (@Html.BeginForm("Index", "Recommendations", FormMethod.Post))
{
<button class="btn btn-large btn-primary" d="btnNext">@ViewBag.TextDisplay</button>
}
这是在构造一个 URL,使其在 html 中显示为 Recommendations/,而将 Index 排除在外(可能是因为它是默认参数。但是 index 方法签名已更改,因此它采用了一个可选参数,它出现导致问题。
[HttpPost]
public async Task<ActionResult> Index(int? enquiryId)
为了解决这个问题,我在我的 route.config 文件中添加了以下内容
routes.MapRoute(
name: "DefaultWithIndex",
url: "Recommendations/{enquiryId}",
defaults: new { controller = "Recommendations", action = "Index", enquiryId= UrlParameter.Optional });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
但这现在已经产生了拦截对 RecommendationsController 中其他方法的任何调用的副作用,并将我重定向回索引页面,即Recommendations/index
那么我该如何更改我的路由配置,以便
recommendations/ 和 recommendations/enquiryId=1 映射到 recommendations/index 但 Recommendations/<other method> 转到 Recommendations/<other method> ?
【问题讨论】:
标签: c# asp.net-mvc routing