0

I have 2 action method and 1 view. I want send post request but with controller name after submit on the view. But open page without controller name.

Controller

public class ProductController : Controller
    {
        [HttpGet("/CreateProduct")]
        public IActionResult CreateProduct()
        {
            Product product = new Product()
            {
                Price = new Price()
            };
            return View(product);
        }
        [HttpPost()]
        public IActionResult CreateProduct(Product product)
        {
            return View();
        }
    }

and Actoin

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

@model MvcDemo.Models.Product

<form asp-controller="Product" asp-action="CreateProduct" method="post">
    <input type="text" asp-for="Name" placeholder="Product Name"/> <br/>
    <input type="number" asp-for="Quantity" placeholder="Quantity"/> <br/>
    <input type="number" asp-for="Price.SimplePrice" placeholder="Price"/> <br/>
    <input type="number" asp-for="Price.DiscountPrice" placeholder="Discount Price"/> <br/>


    <input type="submit"/>
</form>

I want to open Page host/CreateProduct but send post request host/product/CreateProduct after submit.

But not working.

Http Error 405. This page isn’t working

Submit button send post request host/CreateProduct. I know the solution to the problem [HttpPost("/CreateProduct")].

But I'm interested in why it doesn't work.

Startup route configuration

app.UseEndpoints(endpoint =>
            {
                endpoint.MapDefaultControllerRoute();
            });
1
  • If you still struggling with the issue you could have a look on official document here Because your route attribute has not implemented accordingly Commented Apr 4, 2022 at 7:48

1 Answer 1

0

when attribute routing starts from "/" it means that the route starts from root. If you want to start from controller

        [HttpPost("/product/CreateProduct")]
        public IActionResult CreateProduct()

or add attribute to the controller

 [Route("[controller]/[action]")]
public class ProductController : Controller

if you want everything working the way you want , you have to map controller route this way . In this case you will not need an attribute routing at all

app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");

            });
Sign up to request clarification or add additional context in comments.

4 Comments

Yes this is solution but I'm interested in why such overload doesn't work in tag-helpers? Because deleting asp-action and asp-controller and write html action = '/Product/CreateProduct' is work if we do not write [HttpPost("/product/CreateProduct")] in action.
@AlizamanHabibli Because there are rules and everything is working properly according the rules. If you don't like these rules you can send an email to a Microsoft team
Change question. What is reason wrong redirect asp-controller and asp-action?
@AlizamanHabibli What is the difference for you? The most important thing is that you can always finc a way to accomplish your task.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.