![](/img/trans.png)
[英]How to route from one controller action method to another controller action method without displaying controller name in MVC 5
[英]Overload action method but different route template(with controller name and without controller name) in .net core 5 MVC
我有 2 個操作方法和 1 個視圖。 我想發送帖子請求,但在視圖上提交后名稱為 controller。 但是打開沒有 controller 名稱的頁面。
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();
}
}
和肌動蛋白
@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>
我想打開頁面主機/CreateProduct,但在提交后發送帖子請求主機/產品/CreateProduct。
但沒有工作。
Http 錯誤 405。此頁面無法正常工作
提交按鈕發送帖子請求主機/CreateProduct。 我知道問題 [HttpPost("/CreateProduct")] 的解決方案。
但我對為什么它不起作用很感興趣。
啟動路由配置
app.UseEndpoints(endpoint =>
{
endpoint.MapDefaultControllerRoute();
});
當屬性路由從“/”開始時,表示路由從根開始。 如果你想從controller開始
[HttpPost("/product/CreateProduct")]
public IActionResult CreateProduct()
或將屬性添加到 controller
[Route("[controller]/[action]")]
public class ProductController : Controller
如果你想讓一切都按你想要的方式工作,你必須 map controller 這樣走。 在這種情況下,您根本不需要屬性路由
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.