【发布时间】:2019-12-04 11:18:32
【问题描述】:
我正在尝试编写一个新的 Web 应用程序。我创建了一个名为 Company 的新区域。该区域是一个控制器,带有一个调用视图组件的视图。视图组件有一个表单,我试图将其重定向到公司区域内的 HomeController(添加操作)。但是它重定向到localhost/Home/Add?area=Company 而不是localhost/Company/Home/Add
我定义路由的 Startup.cs
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
...
app.UseEndpoints(endpoints =>
{
endpoints.MapAreaControllerRoute(
name: "areas",
areaName: "areas",
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
);
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
提供索引页面的 HomeController
[Area("Company")]
[Route("Company/[controller]/[action]")]
public class HomeController : BaseController
{
public HomeController(IRepositoryWrapper repository, IAppBiz biz, IEmailSender emailSender, UserManager<ApplicationUser> userManager, IHttpContextAccessor httpContext) : base(repository, biz, emailSender, userManager, httpContext)
{
}
[HttpGet]
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Add()
{
throw new NotImplementedException();
}
}
HomeController Index.cshtml 调用 ViewComponent
<div class="container m-1">
<div class="row">
<div class="col">
@await Component.InvokeAsync("CompanyList", new { userID = 1 })
</div>
</div>
</div>
带有“添加”按钮的 ViewComponent Default.cshtml 以便我可以添加新公司(它还提供了现有公司的列表,为简洁起见将其删除。
<form asp-area="Company" asp-action="Add" asp-controller="Home" method="post">
<table>
<tr>
<td class="align-middle">
<button class="btn btn-primary"><i class="fas fa-plus"></i></button>
</td>
</tr>
</table>
</form>
【问题讨论】:
标签: asp.net-core model-view-controller asp.net-mvc-routing