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
Add a comment  | 

2 Answers 2

Reset to default 0

If 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