Dresel / RouteLocalization

RouteLocalization is an MVC and Web API package that allows localization of your attribute routes.
MIT License
67 stars 13 forks source link

ActionModels with AttributeRoutes was expected #66

Closed mdmoura closed 7 years ago

mdmoura commented 7 years ago

I am using RouteLocalization with ASP.NET Core 2.0 and I have the following controller:

  public class HomeController : Controller {
    [HttpGet]
    public IActionResult Index() => View();
  } 

And on Startup I have the following configuration:

    public void Configure(IApplicationBuilder applicationBuilder, IHostingEnvironment hostingEnvironment) {
      applicationBuilder.UseRequestLocalization();
      applicationBuilder
        .UseMvc(routes => { routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}"); });
    } 

    public void ConfigureServices(IServiceCollection services) {      

      services
        .AddMvc(x => { x.Filters.Add(new MiddlewareFilterAttribute(typeof(LocalizationPipeline))); })
                .AddTypedRouting()
                .AddRouteLocalization(x => {
                     x.UseCulture("pt").WhereController(nameof(HomeController)).WhereAction(nameof(HomeController.Index)).TranslateAction("Inicio");        

        })          
        .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
        .AddDataAnnotationsLocalization();    

            services.AddSingleton(x => {
                RequestLocalizationOptions requestLocalizationOptions = new RequestLocalizationOptions() {        
                    DefaultRequestCulture = new RequestCulture("en"),
                    SupportedCultures = new[] { new CultureInfo("en"), new CultureInfo("pt") },
                    SupportedUICultures = new[] { new CultureInfo("en"), new CultureInfo("pt") },
                };
                requestLocalizationOptions.RequestCultureProviders.Insert(0, new RouteDataRequestCultureProvider());
                return requestLocalizationOptions;
      });

    }

But I get the following error when I start the site: ActionModels with AttributeRoutes was expected

What am I missing?

Dresel commented 7 years ago

Seems that an empty HttpGet attribute does not set the AttributeRouteModel property of the Selector model. I don't know if this is intended by ASPNET or a bug. I'll try to find out more about this.

Use HttpGet("") in the meantime.

Dresel commented 7 years ago

I've already opened an issue in the MVC repository to clarify this.

Dresel commented 7 years ago

So the answer from the MVC team:

This is (sadly) by design. An [HttpGet] without a string means conventional routing.

Since RouteLocalization is based on AttributeRouting it's mandatory to use [HttpGet("")].

Do you plan to also use conventional routing?

mdmoura commented 7 years ago

I am planning to use only AttributeRouting so I think it will be fine.

I will post if I find some other problem ...