I tried the following:
I have a shared library (.dll) that contains these files:
Views
Search
PowerSearch.aspx
PowerSearch.aspx.cs
PowerSearch.aspx.designer.cs
The PowerSearch.aspx file contains my html code.
The PowerSearch.aspx.cs file contains this:
using System.Web.Mvc;
using CommonProject.Web.Shared.Controllers;
namespace CommonProject.Web.Shared.Views.Search
{
public partial class PowerSearch : ViewPage<SearchViewData>{}
}
And the designer I catually don't even care about cause it's not used anyways.
Nothing fancy, just a strongly typed view.
I basically pulled an existing, working view out of my asp.net mvc project, put it in a seperate library and changed the namespace to contain the word "Shared" as in "this will be shared amongst several mvc projects".
Then in my original asp.net mvc project I created the same structure, only now the aspx page contains nothing but the asp #Page rule.
The matching cs file contains:
using System.Web.Mvc;
using CommonProject.Web.Shared.Controllers;
namespace CommonProject.Web.DRE.Views.Search
{
public partial class PowerSearch : CommonProject.Web.Shared.Views.Search.PowerSearch { }
}
There are no compile errors and no run time exceptions either. There is only a huge blank page...
Anybody got an idea?
Make sure the assembly (dll) is in your bin folder that you are referencing. Also try adding the assembly namespace to your web.config otherwise your View will not be able to find the inherited page.
<pages>
<namespaces>
<add namespace="CommonProject.Web.Shared"/>
How much of a struggle was it to try to add an ASPX page to a class library?! It's not in the default list when you select 'Add New Item...'.
I know it's OK to store Controllers and Models in a separate library DLL/assembly, but I'm pretty sure it's not as trivial to store or share the Views. The problem is that the default view engine is looking in a specific folder on disk for the view (~/Views/Controller/ViewName.as[pc]x or ~/Views/Shared/ViewName.as[pc]x). With a library DLL, the compiler doesn't really have any idea of what it can do with your ASPX file. It's not code, so unless you have a 'Build Action' set, it's just going to ignore it. There are various 'Built Actions' but I think your only options are 'Copy' and 'Embed As Resource'. Copy isn't going to copy to the folder that you need it to (the Views folder in your ASP.NET MVC web project), although you could possibly write a build script or 'Custom Tool' that did that for you (with a bit of work).
Hammett (of Castle Monorail) fame (and now an MS Employee) came out with a sample that allows you to store Views inside library assemblies using a custom VirtualPathProvider class that is able to dig into the DLL and pull out the View (embedded as a resource). The sample application is just a concept right now, so you might hit some roadblocks, but it appears to work and looks like an exciting direction. You can find it on his blog here: MEF and ASP.NET MVC sample. Download the code and do some exploring.
This blog post ASP.NET MVC Plugins is not by the same author as the one above, but it gives another examination of the topic and points to another post here on StackOverflow where a similar question was asked: Using VirtualPathProvider to load ASP.NET MVC views from DLLs.
I've seen a post from Phil Haaaaaaaaaaaaaaaaaack on storing views in the database. It's using Ruby scripts instead of Web Forms, so I'm not 100% sure if you could adapt his sample to fit your needs or not. Check it out here: Scripting ASP.NET MVC Views Stored In The Database.
What does the source look like for the .aspx file? Make sure you are inheriting your view in the page directive like this:
<%# Page Title="" Inherits="CommonProject.Web.Shared.Views.Search.PowerSearch" %&>
Also, if you don't want to specify the whole namespace in your page directive, you can add this to the <namespaces /> section in your web.config:
<add namespace="CommonProject.Web.Shared.Views.Search"/>
Related
I'm currently playing around with blazor just to test what it is able to do.
I have a main project which will act as the final website to be shown to the user. For the components I have created a class-library for holding bootstrap-based components like a Table which will render the table with bootstrap-class applied.
Because of I will have multiple websites at the end, there will also be shared components between those in another class-library project. This one will also have a component called Table which will render a bootstrap-table from the other shared project with additional handlings for sorting, paging, filtering and so on.
The problem I get is, that there is a naming-conflict which I am not able to resolve.
Lets say the the projects are named Company.Website1 for the final website, Company.Shared.Components for the extended table and Company.Shared.Components.Bootstrap which will hold the bootstrap-components to be consumed by the other shared project.
When I try to create my Table-component in Company.Shared.Components I get the following error
Multiple components use the tag 'Table'
I tried whats been written here but then I got the error
Found markup element with unexpected name 'Table.Table'. If this is intended to be a component, add a #using directive for its namespace
I also tried to alias the using directive without any chance.
The razor-file itself is simply
#using Company.Shared.Components.Bootstrap.Table
<Table></Table>
I guess I would get the same errors if I would use a third-party library which has some components named the same as some already existing in my project. So there must be a workaround which I'm currently not able to see.
If you have a multiple components that share the same name, you can just add the namespace in the tag to specify which one you want to use.
So you could do:
<Company.Shared.Components.Table></Table>
Or
<Company.Shared.Components.Bootstrap.Table></Table>
Source: https://learn.microsoft.com/en-us/aspnet/core/blazor/components/?view=aspnetcore-5.0#namespaces
Had this issue with Blazorise.Icon vs Blazorise.Icons.FontAwesome.Icon - anywhere that imported both Blazorise and Blazorise.Icons.FontAwesome hit a problem
In the end, to keep things clean, I just inherited Blazorise.Icons.FontAwesome.Icon:
//in FAIcon.razor
#inherits Blazorise.Icons.FontAwesome.Icon
#{
base.BuildRenderTree(__builder);
}
and used <FAIcon everywhere
I had this issue after moving a blazor component with code-behind cs file. Moving the whole package into a different folder created a namespace conflict in the cs file because the namespace was not automatically updated while the one from the .razor file seems to reflect the folder structure.
Updating the namespace in the .cs file to match the new folder structure resolved the issue for me
Can anyone help me with a problem? I found some solutions but it didnt worked for me. I have to use a part of xaml code to implement a fuctionality on a service, more exacty I have to open location settings from the service soo, in that way I need some xaml code. How exactly can I reference a .xaml file from views to a .cs from services?
From what you gave us, it looks like LocalizareFarmaciiPage.xaml is a Page. It means that it has a code-behind file (which is a .cs file).
If you want to instantiate your page in order to display it, you must add an using statement followed by the namespace your page file is stored in. I believe the namespace you're looking for is ProjectName.Views, so you want to add
using ProjectName.Views
with ProjectName being replaced by your project name of course. Then you should be able to access the LocalizareFarmaciiPage class:
LocalizareFarmaciiPage page = new LocalizareFarmaciiPage();
page.Show();
Also, if I may, interacting with views from services may not be a good idea, as it breaks the responsibility of the services. I believe that only ViewModels should have this responsibility (and you may want to take a look at the MVVM design pattern for this matter). Services should only be responsible for business-logic matters.
I'm looking at migration strategies for an ASP4/MVC4 application into ASP5/MVC6. Our current implementation relies on loading cshtml views as embedded resources from DLL's, and we use a VirtualPathProvider to find these cshtml files.
Unfortunately, HostingEnvironment.RegisterVirtualPathProvider seems to be gone from MVC6. The closest thing I can find is IEnvironment.WebRootFileProvider, which is an IFileProvider. So I coded up a test class to see if it would work, and unfortunately, the IFileProvider I coded up is never queried for CSHTML files. I get requests for all of the .JS, .CSS, etc files, but no .CSHTML's.
What I'm looking for here is the ability to hook into the razor engine and provide a CSHTML file loaded from an embedded resource (or really, any other source for that matter) when a view is requested. What should I be looking at here?
Thanks!
Alright, I figured it out by digging around in the source code. I can load views from wherever I want by overriding the File Provider on the Razor View Engine options class:
services.AddMvc().AddRazorOptions(x => x.FileProvider = new EmbeddedFileProvider(typeof(Startup).Assembly));
Now the only problem I see is that there appears to be no way to embed resources in an ASP.NET 5 DLL via Visual Studio...
I am developing 2 MVC5 websites. They run seperately, but actually related to each other (1 Admin site, and 1 User site). In both site, I have to render a graphical board using HTML table. I am using PartialView with a Model to render it.
Currently, this is my solution structure:
Project.Common (A class library project, contain the model)
Project.Admin (MVC5 project for the Admin site), refering to Project.Common.
Project.User (MVC5 project for User site), refering to Project.Common.
To render the graphical board, I will use BoardData class from Project.Common, which is ok. But with current solution structure, I have to create 2 PartialView in each MVC5 project, which may be hard to maintain later.
Please suggest a good solution for reusing the PartialView. I already know I can write Helper, but that way, I have to write HTML code inside C# code, which is not very good. Is there any way to do this within Razor View?
One of the options you can use is add a link to a file. In this case keep your Partial view in just single project and in second project use Add -> Existing Item, select your partial view from the first project and click on a small down arrow next to the Add button and choose Add as Link. More info can be found on MSDN, see section Add an existing item as a link.
In this case you will have a partial view only in one project and any changes to it will automatically be applied to other project as well.
You should embed your views into the Project.Common dll, a detailed description can be found on https://visualstudiogallery.msdn.microsoft.com/f28290ce-d987-4f91-b034-707031e10ce6
I'm using asp.net mvc 3 and .net framework 4.0 with C#
Let's say I have NivoSlider( a slider ) html code. Also it has js, css and image files.
I want to use the NivoSlider cshtml code and js/css/images as "a project" and I want to add it to different MVC solutions. It will become a plugin some-how.
I can't make it a partialview, because I have to move all the css, js and imges files into new solution. I looked at "mvc areas" but it's not rendering my js and css files ( as a matter of fact; using "Areas" is not a perfect way as you know )... I looked up some plugin based architectures, but none-of-them are easy to implement. I have limited time.
So how can I solve this problem ?
EDIT: I can use this http://razorgenerator.codeplex.com/ - But I'm looking for another solutions if you came up with an idea...
You could create a private Nuget and create a small installation for it. That way you can install, update, and uninstall directly from the package.
You can even make a localized package that you copy between computers. Nuget has a very easy way to specify where you want to extract files to, and what files you want to extract.
I'm using razor generator in a commercial project and it's functional but not ideal.
Other possibilities I considered are Add files as link (see Nameless One's answer)
Also overriding the ViewEngine
Can I specify a custom location to "search for views" in ASP.NET MVC?
Or even symbolic links (shortcut links to folders in windows)
https://superuser.com/questions/234422/does-windows7-support-symbolic-links-folder-shortcuts
As recommended above, Razor views can be embedded into assembly as compiled class (by using Razor Generator).
Static resources as .html, .js, .png can be located in the assemble as embedded resource and served by application via VirtualPathProvider (custom or use existing one like https://github.com/mcintyre321/EmbeddedResourceVirtualPathProvider)
Compile your asp.net mvc Razor views into a seperate dll
I wanted to be able to embed compiled Razor views in a dll. This would allow for easy distribution of asp.net mvc ‘modules’ that have their default views embedded, but allowing you to place files in your ‘views’ folder to override those default views.
http://www.chrisvandesteeg.nl/2010/11/22/embedding-pre-compiled-razor-views-in-your-dll/