Html.ActionLink 不知道当前区域,生成错误链接

huangapple go评论66阅读模式
英文:

Html.ActionLink not aware of current area generates bad links

问题

上下文

我正在迁移一个运行在.NET 4.7.2上的ASP.NET MVC 5应用到ASP.NET Core 2.2,使用的是.NET 7.0。我正在从一个空项目开始,逐步移动和更改文件。我当前的重点是处理区域(areas)。

位于区域中的Html.ActionLink()助手不会为当前区域生成链接。这是代码之前的行为。它现在会为默认区域生成链接。

这是一个配置错误还是框架中的一个已批准的更改?

我该如何解决这个问题?或者如何更改这种行为以简化迁移?

代码示例

位于文件Areas/Manager/Views/Home/Index.cshtml中的页面/Manager包含:

<p>@Html.ActionLink("Display everything", "Index", new { All = 1, })</p>

这是从"Manager"区域到相同区域的链接。

期望的URL:/Manager?All=1/Manager/Home?All=1/Manager/Home/Index?All=1(同一区域)
当前URL: /?All=1(默认区域)

相应的控制器(Areas/Manager/Controllers/HomeController.cs):

public class HomeController : Controller
{
    public ActionResult Index(string All = null)
    {
         // ...
    }
}

该区域的父控制器(Areas/Manager/Controllers/Controller.cs):

[Area("Manager")]
public class Controller : BaseBackofficeController
{
    // ...(与问题无关的代码)...
}

整个网站的父控制器(Shared/BaseBackofficeController.cs):

public abstract class BaseBackofficeController : Microsoft.AspNetCore.Mvc.Controller
{
    // ...(与问题无关的代码)...
}

Startup.cs中的路由配置:

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");
    endpoints.MapAreaControllerRoute("Manager", "Manager", "Manager/{controller=Home}/{action=Index}/{id?}");
    // 更多与问题无关的路由
});

尝试过的事情

[Area("Manager")]添加到实际控制器而不是父控制器上不会解决问题。

@Html.ActionLink("Display everything", "Index", "Home", new { All = 1, })
=> /?All=1

@Html.ActionLink("Display everything", "Index", "Home", new { All = 1, area = "Manager", })
=> /?All=1&area=Manager
看起来UrlHelper不知道区域信息。

英文:

Context

I am migrating an ASP.NET MVC 5 app (running on .NET 4.7.2) to ASP.NET Core 2.2 on .NET 7.0. I am proceeding from a blank project and slowly moving-and-changing files. My current subject is the areas.

The Html.ActionLink() helper located in an area does not generate links for the current area. This was the previous behavior of the code. It now generates links for the default area.

Is this a configuration error or an approved change in the framework?

How can I fix the problem? Or change this behavior to ease the migration?

Code example

The page at /Manager in file Areas/Manager/Views/Home/Index.cshtml contains:

<p>@Html.ActionLink("Display everything", "Index", new { All = 1, })</p>

This is a link from the "Manager" area to the same area.

Expected URL: /Manager?All=1 or /Manager/Home?All=1 or /Manager/Home/Index?All=1 (same area)
Current URL: /?All=1 (default area)

The corresponding controller (Areas/Manager/Controllers/HomeController.cs):

public class HomeController : Controller
{
    public ActionResult Index(string All = null)
    {
         // ...
    }
}

The parent controller for the area (Areas/Manager/Controllers/Controller.cs):

[Area("Manager")]
public class Controller : BaseBackofficeController
{
    // ...

    public override Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { /* unrelated code */ }
}

The parent controller for the whole website (Shared/BaseBackofficeController.cs):

public abstract class BaseBackofficeController : Microsoft.AspNetCore.Mvc.Controller
{
    // ...unrelated code...
}

The route configuration in Startup.cs:

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

Things I tried

Adding [Area("Manager")] on the actual controller instead of the parent controller does not fix the issue.

@Html.ActionLink("Display everything", "Index", "Home", new { All = 1, })
=> `/?All=1

@Html.ActionLink("Display everything", "Index", "Home", new { All = 1, area = "Manager", })
=> /?All=1&area=Manager
It looks like the UrlHelper is not aware of the areas.

答案1

得分: 1

它看起来UrlHelper不知道这些区域。

这是因为链接匹配了第一个路由模板。

请按照以下方式更改路由模板的顺序:

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

    endpoints.MapAreaControllerRoute("Manager", "Manager", "Manager/{controller=Home}/{action=Index}/{id?}");
    // 更多无关的路由
});
英文:

> It looks like the UrlHelper is not aware of the areas.

That is because the link matches the first route template.

Change the order of your route template like below:

app.UseEndpoints(endpoints =>
{
    endpoints.MapAreaControllerRoute("Manager", "Manager", "Manager/{controller=Home}/{action=Index}/{id?}");

    endpoints.MapControllerRoute(
        name: "default", 
        pattern: "{controller=Home}/{action=Index}/{id?}");
    // more unrelated routes
});

huangapple
  • 本文由 发表于 2023年3月21日 01:12:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75793323.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定