Aspnetcore.docs: Does this work with Razor views?

Created on 1 Jun 2018  Â·  19Comments  Â·  Source: dotnet/AspNetCore.Docs

This is a feature I've been wanting for quite some time. :) I've been trying to make it work by providing Razor views for use in an MVC application, but I'm having trouble getting that to work. From the description, it seems like it should work; am I reading correctly?


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

P4 Source - Docs.ms

Most helpful comment

Thanks for the quick reply! The examples in that document, though, are also for "Razor Pages". What I was trying to do was put a cshtml file in the library (under Home/Test.cshtml), then use a regular controller (in my example HomeController, action Test which is a simple call to View()) to utilize the view in the library.

I guess the question would be better asked "Does this only work for Razor files with an @page directive?"

All 19 comments

Thanks for the quick reply! The examples in that document, though, are also for "Razor Pages". What I was trying to do was put a cshtml file in the library (under Home/Test.cshtml), then use a regular controller (in my example HomeController, action Test which is a simple call to View()) to utilize the view in the library.

I guess the question would be better asked "Does this only work for Razor files with an @page directive?"

Similar to @danieljsummers I can't get the RCL to work with View Component. I am getting an error saying that the view can't be found. I created a view component the way I usually do and added a view under Views/Shared/Components in the RCL project.

The documentation is saying that RCL is suitable for view components and MVC style Razor pages. Is there an example for this ?

@Rick-Anderson This is the folder structure for my RCL project. Nothing fancy. I added a reference to my MVC project that is consuming this class library. Based on my folder structure if there anything that I need to do to tell the MVC project where to look for the view ?
Note: I successfully added the view component to the page and it can find the assembly, It even go to the constructor but it only fails when it tries to find the actual razor view.

folder-structor

@danieljsummers oops, wrong link. Try https://docs.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-2.1&tabs=visual-studio#scaffold-identity-into-an-mvc-project-without-existing-authorization

@rahamohebbi create the simplest possible project that reproduces the problem and I'll see if I can get someone to look at it. I know it works with view components though.

I've tried several different spots, but still can't make it. I've made a repro of it with 4 different patterns, none of which work.

(This is low priority; I'm sure it's something I'm doing wrong, and I truly do appreciate the help.)

BTW, I'd also be willing to strip it down further (remove JS and all that) and make a similar example as the one with the pages.

@pranavkm can you comment on problems with VC's in RCLs?

ASP.NET Core discovers view components from all registered application parts and ViewComponent views should compile along with other views in the project just fine.

@danieljsummers - try changing the name of your secondary project. The file name that the class library Rcl.Views produces collides with the Views dll that the RazorSdk produces for the Rcl project.

@rahamohebbi a repro app would help.

@pranavkm That's it! :) Works like a charm now.

Would this page be the right place for a warning about this? I'll do up a PR if so.

@Rick-Anderson @pranavkm thanks
I created a brand new asp net core application and added a brand new class library (RCL). everything worked really well so started digging a little more comparing it with my main application.

The only difference i noticed was the RCL project file. copy pasted it to my existing class library and everything worked as it should. We might have some bugs there that needs to be ironed out.

Cheers

@rahamohebbi thanks for the update.

@pranavkm can you respond to @danieljsummers

Would this page be the right place for a warning about this? I'll do up a PR if so.

Is that worth warning about?

While you're considering it, my thoughts were, under the "Create a class library containing Razor UI" heading:

  • On the "Visual Studio" tab, adding a bullet before the last one, saying something like "Name the library (ex. "RazorUIClassLib"). _(Ensure the name does not end with .Views to avoid a namespace collision.)_"
  • On the .NET Core CLI tab, adding the same italicized text after the link that provides the link to documentation for dotnet new.

OTOH, if the namespace collision shouldn't be an issue (i.e., a bug), then I'd think the doc either wouldn't need to be updated, or could be updated now, and then reverted once the fix is in place.

@pranavkm can you comment on the suggestion by @danieljsummers ?

This seems reasonable. Also, it's an issue with file name collision rather than a namespace collision.

I'll get it turned tomorrow evening (Sunday) unless something comes up IRL.

I ended up moving the bullet up 1 from my original suggestion, because the name of the project precedes the dialog that has the .NET Core 2.1 selection. I also spelled out "filename collision with the generated view library" vs. "namespace collision".

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nenmyx picture nenmyx  Â·  3Comments

Raghumu picture Raghumu  Â·  3Comments

YeyoCoder picture YeyoCoder  Â·  3Comments

danroth27 picture danroth27  Â·  3Comments

Rick-Anderson picture Rick-Anderson  Â·  3Comments