-1

I have CSS, Javascript, classes and partial views for a grid with lots of sorting and filtering options. I'd like to include it all in a shared project in Visual Studio so that I can reuse it in different apps.

The CSS, Javascript, and classes for the grid work correctly in the shared project. The partial views do not. I get a lot of errors on the partial views, and I'm not sure how to reference the partial views from the views in my web app.

Is it possible to include views in a shared project? How do I do it if it is possible? Thanks!

t_m27
  • 103
  • 16
  • Does this question https://stackoverflow.com/questions/31920735/how-to-use-partial-view-from-another-project-in-asp-net-mvc do what you want? – GoldenAge Sep 12 '18 at 19:58
  • That is pretty much what I want to do. I have tried several of the RazorGenerator examples online and haven't been able to get them to work. When I try to provide the full path and use the ../, I get an error stating that I cannot use a leading .. to exit above the top directory. – t_m27 Sep 12 '18 at 20:28

1 Answers1

0

RCLs

One method of doing this that may be right up your alley is to use a Razor Class Library (RCL). RCL's are fairly new to ASP.NET and provide a method of easily sharing and reusing razor views.

Please refer to the documentation below:

https://learn.microsoft.com/en-us/aspnet/core/razor-pages/ui-class?view=aspnetcore-2.1&tabs=visual-studio

With RCLs you can override the shared views in your web project should you want to.

Applications can include the RCL and override the views and pages it contains. 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.

Shared Source Code

You could also share the razor source code using your version control in order to ensure that each project has the same views. I personally wouldn't recommend this one but it is an option.

Precompiled Views

Another option since you're not using .NET Core is to pre-compile your views. You can use the following article for instructions:

https://www.codeproject.com/Articles/1169354/Pre-compiled-Razor-View-in-ASP-NET-MVC

After obtaining the DLL holding your compiled views you can easily include this as a reference in other projects.

Other options

Check out this answers in this question for further options:

Sharing Razor views across projects

DCCoder
  • 1,587
  • 4
  • 16
  • 29
  • I'm not using .NET Core, so I don't think RCLs are an option. I'll look into the other options at the link. – t_m27 Sep 12 '18 at 20:15
  • @t_m27 Another option would be to pre-compile your views. I edited my answer to add this to it. – DCCoder Sep 12 '18 at 20:51