LazZiya / XLocalizer

Localizer package for Asp.Net Core web applications, powered by online translation and auto resource creating.
https://docs.ziyad.info
128 stars 14 forks source link

AmbiguousMatchException: The request matched multiple endpoints #38

Open LazZiya opened 1 year ago

LazZiya commented 1 year ago

Originally posted by @Malkawi1 in https://github.com/LazZiya/XLocalizer/issues/2#issuecomment-1328900840

hi sir,

how to edit this code ->

// add culture route segment for controllers e.g. /en/Home/Index
    .AddMvcOptions(ops => ops.Conventions.Insert(0, new RouteTemplateModelConventionMvc()))

because make this error:


 An unhandled exception occurred while processing the request.
AmbiguousMatchException: The request matched multiple endpoints. Matches:

Microsoft.AspNetCore.OData.Routing.Controllers.MetadataController.GetServiceDocument (Microsoft.AspNetCore.OData)
Microsoft.AspNetCore.OData.Routing.Controllers.MetadataController.GetMetadata (Microsoft.AspNetCore.OData)
Link.Controllers.GroupsController.ConfirmEmail (Link)
Link.Controllers.HomeController.Error (Link)
Link.Controllers.HomeController.ChatBot (Link)
Link.Controllers.HomeController.Index (Link)
Link.Controllers.GroupsController.SendRequest (Link)
Link.Controllers.CommentsController.Index (Link)
Link.Controllers.GroupsController.Create (Link)
Link.Controllers.GroupsController.Details (Link)
Link.Controllers.GroupsController.Groups (Link)
Link.Controllers.GroupsController.Index (Link)
Link.Controllers.CommentsController.Hashtag (Link)
Link.Areas.Admin.Controllers.HomeController.Index (Link)
Link.Controllers.GroupsController.Edit (Link)
Link.Controllers.PeopleController.Index (Link)

Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.ReportAmbiguity(CandidateState[] candidateState)



_Originally posted by @Malkawi1 in https://github.com/LazZiya/XLocalizer/issues/2#issuecomment-1328900840_
LazZiya commented 1 year ago

@Malkawi1

Check if you have a culture paramter setup already made in page route, if so remove it because the code below is already adding the culture parameter to all routes.

.AddMvcOptions(ops => ops.Conventions.Insert(0, new RouteTemplateModelConventionMvc()))

Or delete the above line if you want to use your own culture parameter setup.

Malkawi1 commented 1 year ago

@LazZiya

After remove this line -> .AddMvcOptions(ops => ops.Conventions.Insert(0, new RouteTemplateModelConventionMvc()))

when choosing language -> error

This localhost page can’t be foundNo webpage was found for the web address: https://localhost:44349/ar HTTP ERROR 404

LazZiya commented 1 year ago

After removing that line you need to manually add the culture parameter to the routes {culture}

Malkawi1 commented 1 year ago

add to this line -> like this <language-nav cookie-handler-url="@Url.Action("SetCultureCookie", "Home", "{culture}",new { area="", cltr="{0}", returnUrl="{1}", culture=CultureInfo.CurrentCulture.Name })">

LazZiya commented 1 year ago

So if it is ok now we can close this issue, is it?

Malkawi1 commented 1 year ago

@LazZiya

sir, when using the package LazZiya.ExpressLocalization

after running the code shown in this route -> https://localhost:44349/en/Identity/Account/Login/?ReturnUrl=/en/

so, this route is wrong and this true route -> https://localhost:44349/Account/Login/?culture=en

so how to edit my route to be true

note I'm using .net 6

and this is my code in program.cs

CultureInfo us = new CultureInfo("en-US"); var cultures = new CultureInfo[] { new CultureInfo("en"), new CultureInfo("ar") { DateTimeFormat = us.DateTimeFormat } };

// Add services to the container. builder.Services.AddControllersWithViews()

.AddExpressLocalization<LocSource>(
        ops =>
        {
            ops.ResourcesPath = "LocalizationResources";
            ops.RequestLocalizationOptions = o =>
            {
                o.SupportedCultures = cultures;
                o.SupportedUICultures = cultures;
                o.DefaultRequestCulture = new RequestCulture("en");
            };

        });

app.UseRequestLocalization();

LazZiya commented 1 year ago

Well, I see you are using LazZiya.ExpressLocalization, I recommend you switch to XLocalizer, it is much better in terms of flexibility and provides better support for newer frameworks.

Malkawi1 commented 1 year ago

After removing that line you need to manually add the culture parameter to the routes {culture}

@LazZiya

how invested this lin by parameter {culture}
.AddMvcOptions(ops => ops.Conventions.Insert(0, new RouteTemplateModelConventionMvc()))

please tell me step by step

LazZiya commented 1 year ago

With reference to XLocalizer version of RouteTemplateModelConventionMvc(), it adds a route parameter named {culture?} to all routes in the app, so you don't have to add it manually to each route in the routing table.

// e.g.:
[controller]/[action] --> {culture?}/[controller]/[action]
LazZiya commented 1 year ago

Hi @Malkawi1 ,

Do you still need support with this issue?