We are in the process of upgrading an ASP.NET Core 2.2 project to ASP.NET 3.0 that uses EndPoint routing.
We have a large list of urls constructed with Url.RouteUrl using named routes, e.g. :
string url = Url.RouteUrl("blog-details", new { title = item.Title, id = item.Id });
// returns correct link of https://example.org/us/blog/some-title-6 in 2.2 but is blank in 3.0
[Route("~/{lang}/blog/{title}-{id}", Name= "blog-details")]
public async Task<IActionResult> Details(string title, int id)
{
}
After upgrading to 3.0 those urls just produces a blank href. Our startup.cs is looking like this:
public void ConfigureServices(IServiceCollection services)
{
...
services.AddControllersWithViews(options =>
{
options.Filters.Add(new MiddlewareFilterAttribute(typeof(LocalizationPipeline)));
})
.AddViewLocalization(LanguageViewLocationExpanderFormat.SubFolder)
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix);
services.AddRazorPages();
...
}
We have tried replacing with below but that creates the wrong link and does not allow us to reuse as a variable, e.g.:
<a asp-action="Details" asp-controller="Blog" asp-route-title="item.Title" asp-route-id="@item.Id">Link here</a>
url = Url.Action("Details", "Blog", new { id = item.Id, title = item.Title });
url = Url.RouteUrl(new { action = "Details", controller = "Blog", id = item.Id, title = item.Title });
// all returns https://example.org/us/blog/details/6?title=some-title
<a asp-controller="Home" asp-action="Pricing">Pricing</a>
// returns https://example.org/us/home/pricing instead of correct https://example.org/us/pricing
[Route("~/{lang}/pricing")]
public async Task<IActionResult> Pricing()
{
...
}
This works, however:
<a asp-controller="Signup" asp-action="Customer">Sign up</a>
// returns correct https://example.org/us/signup/customer
[Route("~/{lang}/signup/customer")]
public IActionResult Customer()
{
...
}
What are we doing wrong if we want to use EndPoint routing (not the old 2.2.-way)?
langis null after migration to 3.0 app , check Globalization and localization in your 3.0 app.options.Filters.Add(new MiddlewareFilterAttribute(typeof(LocalizationPipeline)));to avoid decorating all controllers. Also addedservices.AddRouting(options => { options.ConstraintMap.Add("lang", typeof(LanguageRouteConstraint)); });