0

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
bperniciaro
  • 901
  • 2
  • 12
  • 30

1 Answers1

1

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

enter image description here

It's ok now,when I debug:

enter image description here

Ruikai Feng
  • 6,823
  • 1
  • 2
  • 11
  • While the URL appears to contain the correct path, I'm still hitting the Home/Index route when debugging. https://app.screencast.com/ga29CTBrXtCSB – bperniciaro Jul 14 '23 at 02:20
  • Not sure if this is a clue, but if I try to redirect to the route I want, I get this error: https://app.screencast.com/iblF5EGcj3duY But that makes no sense because this is all one application. Very confused. – bperniciaro Jul 14 '23 at 02:28
  • 1
    Try as below: Close Vs, delete your .vs floder,open your solution and debug – Ruikai Feng Jul 14 '23 at 02:31
  • 1
    Here's a related issue :https://stackoverflow.com/questions/58246822/http-error-500-35-ancm-multiple-in-process-applications-in-same-process-asp-ne it's related with visual studio itself – Ruikai Feng Jul 14 '23 at 02:37
  • Yes this definitely one of my problems – bperniciaro Jul 14 '23 at 02:55
  • Editing the program.cs and launchsettings.json directly didn't work. I keep getting that 500.35 error requiring deleting the /vs folder. However, editing the project settings directly as you suggested resolved the problem and it is working now without the redirect. Thanks! – bperniciaro Jul 14 '23 at 02:58