asp.net-core.net-coreasp.net-core-mvcasp.net-core-2.2asp.net-routing

Routing Issues in .NET core After Upgrade


I have upgraded the solution from 2.2 to 3.1. Everything was running smoothly in 2.2 . After the upgrade, i have got routing issues.

It is unable to route to a controller of a different project in the solution.

With 2.2, i was using

 app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "areaRoute",
                    template: "{area:exists}/{controller=Home}/{action=Index}");

                routes.MapRoute(
                    name: "default",
                    template: "{controller=Account}/{action=Login}/{id?}");

            });

I tried changing it to

app.UseRouting();

 app.UseEndpoints(endpoints =>
            {
                endpoints.MapAreaControllerRoute(
                    name: "Quote",
                    areaName: "Quote",
                    pattern: "Quote/{controller=Home}/{action=Index}/{id?}");


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

                endpoints.MapRazorPages();
                endpoints.MapControllers();
                endpoints.MapControllerRoute("default", "/{action}/{id?}", new { action = "Index", controller = "Home" });
            });

But it didn't help. I am loading assemblies but doesn't seem to be working with 3.1 version of .NET core.

  var mvcBuilder = services.AddMvc(config =>
            {
                var policy = new AuthorizationPolicyBuilder()
                                 .RequireAuthenticatedUser()
                                 .Build();
                config.Filters.Add(new AuthorizeFilter(policy));
            });


 foreach (var assemblyPlugin in pluginsInfoList)
            {
                // Register controller from modules
                mvcBuilder.AddApplicationPart(assemblyPlugin.Assembly);
            }

            mvcBuilder.AddControllersAsServices();
            mvcBuilder.AddViewComponentsAsServices();


 services.Configure<RazorViewEngineOptions>(options =>
            {
                options.ViewLocationExpanders.Add(new SynapseCore.Services.ThemeControl.TenantViewLocationExpander());
                options.AreaViewLocationFormats.Add("/Areas/{2}/Views/{1}/{0}.cshtml");
                options.AreaViewLocationFormats.Add("/Areas/{2}/Views/Shared/{0}.cshtml");

            });

            services.Configure<MvcRazorRuntimeCompilationOptions>(options =>
            {


                foreach (PluginInfo ioptionPlugin in pluginsInfoList)
                {

                    options.FileProviders.Add(new EmbeddedFileProvider(
                        ioptionPlugin.Assembly,
                        baseNamespace: ioptionPlugin.Name.Replace(".dll", "")));
                }
            });


I get the error that it tried to look for that action but couldn't find followed by the list of pages where it looked for the particular action. Which indicates it hasn't looked in the controller of other projects.


Solution

  • It's working now. I added the following lines of code.

    IMvcBuilder mvcBuilder = services.AddRazorPages(); mvcBuilder.AddRazorRuntimeCompilation();

    This resolved the issue and added the dlls of other projects and now it's routing to controllers of other projects.