【问题标题】:Issue with routing to Areas Net Core 3 MVC路由到 Areas Net Core 3 MVC 的问题
【发布时间】: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


    【解决方案1】:

    Startup.cs找到它...

      endpoints.MapAreaControllerRoute(
                        name: "areas",
                        areaName: "areas",
                        pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
                        );
    

    应该是

    endpoints.MapAreaControllerRoute(
                        name: "area",
                        areaName: "area",
                        pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
                        );
    

    【讨论】:

    • 谢谢你。我读过的大多数文档都说“区域”而不是“区域”。
    猜你喜欢
    • 2011-09-14
    • 2020-04-17
    • 2021-01-11
    • 2018-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多