Getting right view from two razor libraries

Niki Kante 1 Reputation point
2021-02-10T19:31:08.66+00:00

I'm writing ASPNET Core web page.
I created two razor view libraries and in each one I created file List.cshtml and controller:
WebPage.Users
List.cshtml
UsersController.cs
WebPage.Clients
List.cshtml
ClientsController.cs

Both controllers have Index method:

public class ClientsViewController : Controller
{
    public IActionResult Index()
    {
        return this.View("~/Pages/Views/List.cshtml");
    }
}

public class UsersViewController : Controller
{
    public IActionResult Index()
    {
        return this.View("~/Pages/Views/List.cshtml");
    }
}

StartUp code:

 public void ConfigureServices(IServiceCollection services)
 {
        services.AddControllersWithViews();

        services.AddMvc()
            .AddApplicationPart(typeof(UsersController).Assembly)
            .AddApplicationPart(typeof(ClientsController).Assembly);
 }

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }
        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseRouting();

        app.UseAuthorization();
        app.UseEndpoints(endpoints =>{
            endpoints.MapControllerRoute(
                name: "usersViewController",
                pattern: "modules/users/{action}",
                defaults: new {
                    controller = "UsersView",
                    action = "Index"
                });
            endpoints.MapControllerRoute(
                name: "clientsViewController",
                pattern: "modules/clients/{action}",
                defaults: new {
                    controller = "ClientsView",
                    action = "Index"
                });
        });
     }

Now the problem is when I open page "modules/clients/" in browser
it returns view from WebPage.Users.Views. I want to return view from WebPage.Clients.Views assembly.
How can I do this?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,256 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,418 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Niki Kante 1 Reputation point
    2021-02-11T09:35:46.717+00:00

    Thanks for comment.
    There is view "~/Pages/Views/List.cshtml" in both assemblies.

    Expected result is:
    ClientsViewController should return view from assembly WebPage.Clients.Views.
    UsersViewController should return view from assembly WebPage.Users.Views.

    Is there a way to accomplish that? Renaming/changing path is not an option.

    0 comments No comments

  2. Michael Wang-MSFT 1,051 Reputation points
    2021-02-18T07:28:27.867+00:00

    Hi, @Niki Kante ,

    We can see the reasons from the description of Razor Class Library.

    Override views, partial views, and pages

    When a view, partial view, or Razor Page is found in both the web app and the RCL, the Razor markup (.cshtml file) in the web app takes precedence. For example, add WebApp1/Areas/MyFeature/Pages/Page1.cshtml to WebApp1, and Page1 in the WebApp1 will take precedence over Page1 in the RCL.

    You could use Area to separate these two RCLs.

    ------
    If the answer doesn’t solve your issue, please provide more details of error that will help us track down what’s happening.
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,
    Michael Wang

    0 comments No comments