在ASP.NET Core 7 MVC Web应用程序中,如何根据区域在特定路径启动调试会话?

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

In an ASP.NET Core 7 MVC web application, how do you start a debugging session at a specific path based on an area?

问题

在我的ASP.NET Core 7 MVC Web应用程序中,我正在使用IIS Express进行调试。当应用程序启动时,我希望加载的第一个路由是:

https://localhost:44341/fantasy-football/create/cheatsheet/edit/1645168

这样可以加快调试速度,因为我不必从主页开始,然后点击各种页面才能到达我正在测试的路由。

这个URL路由映射到一个位于区域内的控制器:

/Areas/FantasyFootball/Controllers/Create/CheatSheetController.cs

而操作方法的签名如下:

[Route("/fantasy-football/create/cheatsheet/edit/{id?}", Name = "fantasyfootball.create.cheatsheet.edit")]
public IActionResult Edit(int id)

我尝试更新launchSettings.json文件中的应用程序URL,但它仍然加载在Program.cs中定义的默认路由。

"iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
        "applicationUrl": "http://localhost:51696/fantasy-football/create/cheatsheet/edit/1645168",
        "sslPort": 44341
    }
},

我尝试更新Program.cs中的默认路由:

app.MapControllerRoute(
    name: "default",
    pattern: "{area=FantasyFootball}/{controller=CheatSheet}/{action=Edit}/{id=1645168}");

这似乎在浏览器中加载了正确的URL,但我收到了404错误。

英文:

In my ASP.NET Core 7 MVC web application, I am debugging using IIS Express. When the application starts, I want the first route to load to be:

https://localhost:44341/fantasy-football/create/cheatsheet/edit/1645168

This makes it faster to debug as I don't have to start at the home page, then click through various pages to get to the route I'm testing.

This URL route maps to a controller housed in an area:

/Areas/FantasyFootball/Controllers/Create/CheatSheetController.cs 

And the action method signature is this:

[Route("/fantasy-football/create/cheatsheet/edit/{id?}", Name = "fantasyfootball.create.cheatsheet.edit")]
public IActionResult Edit(int id)

I have tried to update the application url in the launchSettings.json file, but it still loads the default route defined in Program.cs.

"iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
        "applicationUrl": "http://localhost:51696/fantasy-football/create/cheatsheet/edit/1645168",
        "sslPort": 44341
    }
},

I tried updating the default route in Program.cs:

app.MapControllerRoute(
    name: "default",
    pattern: "{area=FantasyFootball}/{controller=CheatSheet}/{action=Edit}/{id=1645168}");

And this seems to load the correct URL in the browser, but I get a 404 error.

答案1

得分: 1

Sure, here's the translated code portion:

请尝试如下:

//区域属性很重要
[Area("fantasy-football")]
public class CheatSheetController : Controller
{
    //移除[Route]属性,它将覆盖在Program.cs中注册的路由模式
    public IActionResult Edit(int id)
    {
        return Ok();
    }               
            
}

Program.cs中

app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapAreaControllerRoute(
"fantasy",
"fantasy-football",
pattern: "{area}/Create/{controller}/{action}/{id?}"
);

保留

"applicationUrl": "http://localhost:51696

launchsetting.json中添加

"IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "fantasy-football/create/cheatsheet/edit/1645168",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }

在配置文件部分

或右键单击项目=>属性=>调试=>打开调试启动配置UI

[![enter image description here][1]][1]

现在调试时没问题:

[![enter image description here][2]][2]

[1]: https://i.stack.imgur.com/f4qdy.png
[2]: https://i.stack.imgur.com/WwraS.gif

Please note that I've provided the translation for the code part as requested. If you need any further assistance or have specific questions about the code, feel free to ask.

英文:

Please try as below:

//Area attribute is important
[Area("fantasy-football")]
    public class CheatSheetController : Controller
    {
        //Remove the [Route] Atrribute which would which would override the route parttern you registed in Program.cs
        public IActionResult Edit(int id)
        {
            return Ok();
        }               
        
    }

in Program.cs:

    app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapAreaControllerRoute(
    "fantasy",
    "fantasy-football",
    pattern: "{area}/Create/{controller}/{action}/{id?}"
    );

Keep

"applicationUrl": "http://localhost:51696

in your launchsetting.json

add

"IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "fantasy-football/create/cheatsheet/edit/1645168",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }

in profile section

or right click on your project=>properties=>debug=>open debug launch profiles UI

在ASP.NET Core 7 MVC Web应用程序中,如何根据区域在特定路径启动调试会话?

It's ok now,when I debug:

在ASP.NET Core 7 MVC Web应用程序中,如何根据区域在特定路径启动调试会话?

huangapple
  • 本文由 发表于 2023年7月13日 12:45:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76676009.html
匿名

发表评论

匿名网友

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

确定