TimHorton
TimHorton

Reputation: 885

ASP.NET Core 2.1. Razor UI Class Library in MVC View

I'm currently working on an ASP.NET Core 2.1. RC1 Final (May 2018) demo application. I try to use the new Razor UI Library in my ASP.NET Core MVC application.

Thus, in my Solution I have 2 Projects:

  1. ASP.NET Core MVC application
  2. Razor Class Library

The structure of my Razor Class Library is very simple:

The cshtml of Page1.chtml looks like that:

@page
@model RazorClassLib1.MyFeature.Pages.Page1Model
@{
    Layout = "_Layout";
}

<h1>Hello From the Razor ClassLib1</h1>

Now I want to use this Page1.cshtml Razor component in my MVC View. This feature would be useful to organize and reuse razor pages within my application.

Unfortunately I have no idea how to achieve this. I was following this example:

http://www.talkingdotnet.com/asp-net-core-2-1-razor-ui-as-class-library/

though it seems to work only for a razor page project, but I would need it in my MVC projct.

Do you know how to use a Razor Class Library in ASP.NET MVC Core 2.1. Views?

Thank you very much.

Upvotes: 2

Views: 1874

Answers (1)

PBo
PBo

Reputation: 519

https://gunnarpeipman.com/aspnet/razor-class-library-mvc/

explains how to set it up.

There are some things to know: Controllers must have AreaAttribute Web application must have area route defined.

I think there work arounds using the areas, but I am researching this myself. will update the answer when I have figured it out.

Below link also explains how to make the area name dynamic: https://blog.tech-fellow.net/2018/11/11/razor-ui-class-library-with-dynamic-area-name/

Upvotes: 0

Related Questions