Home > Net >  Get Project name in URL
Get Project name in URL

Time:03-14

When I develop locally (.NET 6.0 - MVC) I have the following base URL:

https://localhost:7221/<controller>

But when I publish it I have the following base URL:

https://<server name>/<application>/<controller>

I can't figure out how to get it the same way locally, e.g.

https://localhost:7221/<application>/<controller>

I'm having problems with AJAX requests who will only work in one of the environments, if I call them with a similar URL:

url: "/Ajax/..."

CodePudding user response:

Do you mean add project name in the URL?

If so, add project name in route in Program.cs

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

Result:

enter image description here

I'm having problems with AJAX requests who will only work in one of the environments, if I call them with a similar URL:url: "/Ajax/..."

[Note] You need to add project name in url too when you use ajax.

CodePudding user response:

You can use the Url.Action helper method which generates a fully qualified URL to an action method for the specified action name and route values. So in your case, you can use it in your AJAX url call like this:

url: '@Url.Action("ActionMethod", "ControllerName")'
  • Related