区域未传递给ASP.net Core中的Url.Action() [英] Area is not passed to Url.Action() in ASP.net Core

查看:81
本文介绍了区域未传递给ASP.net Core中的Url.Action()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码在正常的ASP.net MVC中工作.

The following code is working in normal ASP.net MVC.

Url.Action("actionName", "controllerName", new { Area = "areaName" });

但是在ASP.net Core中它不能很好地工作.Area被识别为查询字符串参数.

But it is not work well in ASP.net Core. Area is recognized as a query string parameter.

我该如何解决?感谢您的帮助.

How can I solve it? Thanks for any help.

推荐答案

请确保您正在注册以下路由:

Make sure you're registering routes as below :

app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "areaRoute",
        // if you don't have such an area named as `areaName` already, 
        //    don't make the part of `{area}` optional by `{area:exists}`
        template: "{area}/{controller=Home}/{action=Index}");  

    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}");
});

我可以通过更改路线顺序或将 {area} 更改为可选内容来重现与您相同的问题.

I could reproduce the same issue as yours by changing the order of routes, or by changing the {area} to be optional.

  1. 订单很重要. areaRoute 应该位于第一位.不要更改顺序.
  2. 生成区域的url时,如果您还没有这样的区域,请不要将 {area} 的部分更改为可选通过 {area:exists} .例如,假设您尝试生成一个 MyAreaName :

  1. The Order matters. The areaRoute should come first. Don't change the order.
  2. When generating url to an area, if you don't have such an area already, don't change the part of {area} to be optional by {area:exists}. For example, let's say you're trying to generate a url to area of MyAreaName:

Url.Action("actionName", "controllerName", new { Area = "MyAreaName" });

如果您的项目中没有名为 MyAreaName 的区域,并且您已通过以下方式将该区域设置为可选:

If there's no area named as MyAreaName in your project, and you've made the area optional by:

routes.MapRoute(
    name: "areaRoute",
    template: "{area:exists}/{controller=Home}/{action=Index}");

生成的网址将是 controllerName/actionName?Area = MyAreaName .

这篇关于区域未传递给ASP.net Core中的Url.Action()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆