admin管理员组文章数量:1288020
I have the following setup created:
app.MapControllerRoute(
name: "Product",
pattern: "product/{*url}",
defaults: new { controller = "Product", action = "Index" });
public class ProductController : BaseStoreController
{
[HttpGet]
public IActionResult Index(string url)
{
....
}
[ValidateAntiFeryToken]
[HttpPost]
public IActionResult AddProduct(ProductViewModel model)
{
....
}
}
@using (Html.BeginForm("AddProduct", "Product", FormMethod.Post))
{
......
<button type="submit" class="btn btn-danger ms-2">Add</button>
}
The generic routing is forcing all GET
and POST
requests to hit the Index
GET
action. I tried adding the following (in different variations) above the generic routing declaration, but still the Index
GET
action is triggered. I also tried creating an Index
POST
action and having the form POST
to it instead of the AddProduct
action, but still the Index
GET
action is always triggered on any GET
or POST
method.
Any help would be appreciated.
app.MapControllerRoute(
name: "AddProduct",
pattern: "product/addproduct",
defaults: new { controller = "Product", action = "AddProduct" });
I have the following setup created:
app.MapControllerRoute(
name: "Product",
pattern: "product/{*url}",
defaults: new { controller = "Product", action = "Index" });
public class ProductController : BaseStoreController
{
[HttpGet]
public IActionResult Index(string url)
{
....
}
[ValidateAntiFeryToken]
[HttpPost]
public IActionResult AddProduct(ProductViewModel model)
{
....
}
}
@using (Html.BeginForm("AddProduct", "Product", FormMethod.Post))
{
......
<button type="submit" class="btn btn-danger ms-2">Add</button>
}
The generic routing is forcing all GET
and POST
requests to hit the Index
GET
action. I tried adding the following (in different variations) above the generic routing declaration, but still the Index
GET
action is triggered. I also tried creating an Index
POST
action and having the form POST
to it instead of the AddProduct
action, but still the Index
GET
action is always triggered on any GET
or POST
method.
Any help would be appreciated.
app.MapControllerRoute(
name: "AddProduct",
pattern: "product/addproduct",
defaults: new { controller = "Product", action = "AddProduct" });
Share
Improve this question
edited Feb 24 at 13:06
marc_s
755k184 gold badges1.4k silver badges1.5k bronze badges
asked Feb 24 at 13:03
JohnPete22JohnPete22
5233 silver badges17 bronze badges
2 Answers
Reset to default 0If you're "AddProduct" method were called "Index" I'd expect that your first route would work for you. Also, if you were willing to use the differing url "/product/addproduct", I'd expect for your AddProduct method to be called via your second route example.
Since you state you tried both methods, then I'm thinking that perhaps [ValidateAntiFeryToken] is failing and actually causing a redirect to /Index which would then be a GET. I'd use Telerik Fiddler (or possibly Chrome or Edge's Network tab) to see whether there was actually a redirect. I'd also probably try the program with the ValidateAntiFery commented out briefly.
If it turns out to be the case that the Anti Fery is causing a problem, you'll have to read up on how to render your form properly in the View (I can't remember how it's done).
Mark
all uri like "/product/a"
,"/product/b"
would match the parttern: pattern: "product/{*url}"
,
also, the both index
action and addproduct
action would match the parttern,but index action has higher priority than addproduct
action,so index
action would always been hit
see this part of document
I tried adding the following (in different variations) above the generic routing declaration, but still the Index GET action is triggered.
since the first parttern could match the url,the second parttern would be ignored,see this part of document
本文标签: cASPNET Core MVCMapControllerRoute with generic pattern not allowing POSTStack Overflow
版权声明:本文标题:c# - ASP.NET Core MVC : MapControllerRoute with generic pattern not allowing POST - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741268461a2368892.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论