Pack my ASP.NET MVC pager into NuGet package? - asp.net-mvc

I've a pager functionality I use in some of my ASP.NET MVC projects. It consists of a couple of classes and interfaces (to generalize approach to paging of different datasources) and .aspx or .chtml view.
I can put my C# code into NuGet package/library assembly but what to do with view files which can be different for different projects (different pager design, for example)? One approach is to put HTML to HTML helper, written in C#, but I don't like putting bit chunks of html into C# code.
Is it possible to put views to NuGet package or should I use recipe feature?

I've wanted to push the usage of Nuget in MVC apps to reuse MVC apps themselves not just code - views too. I did manage to pack it up but it has like dozens issues and the experience is horrible.
If you have a pager functionality built inside an ASP.NET MVC Web application project then you can just pack it up and it will pick up the Views too. It does become a lot trickier when using DI, when using Areas, when using localization (resource files don't get picked up). For instance, Area routes might get compiled and included, on the other hand the main global.asax.cs will never be included in the package (so you have to manually copy the routes over if you have them in there).
But it does include stylesheets, so you can style your pager to the default stylesheet which you include in your package and that way you have also solved the issue of design modifications.
But all in all NuGet isn't best suited to pack MVC (or classic ASP.NET webforms) apps.

Related

System.Web.Helpers dll

In Visual Studio 2019 for Razor pages for nuGet I see the following
System-Web-Helpers.dll
AD.System.Web.Helpers.dll
the descriptions are pretty clear but what makes me pause is the number of downloads 198k and 88k.
That seems really low for basically something I would think 100% of the Razor community would need so I am not confident that I not using the dll that most people are using.
I assume this is also being used for MVC so I would assume downloads would be in the millions.
System.Web.Helpers was released as part of ASP.NET Web Pages in 2010. It contains helpers for common tasks such as working with charts, images, caching as well has being the home of the WebGrid. While it is available from Nuget, the standard way that it was made available to Web Pages and MVC applications (MVC 3-5) was as part of the project template - hence the relatively low download numbers.
It depends on System.Web, which is part of the old .NET Framework, and is therefore not suitable for use in Razor Pages or any other type of .NET Core application.

ASP MVC nuget packages are not added to the correct folders

I've added two well known projects to an ASP MVC application and it looks like the packages don't install under /Scripts and /Content as expected (CSS and JS are bundled together).
Am I missing something? Do package authors assume that people will move content to the correct folders, or does no one use the default ASP MVC template folders?
E.g.
Am I missing something?
Nope.
Do package authors assume that people will move content to the correct folders, or does no one use the default ASP MVC template folders?
Pretty much both.
There is no guarantee that packages will properly conform to the default ASP.NET MVC template.
The authors have probably done it because
As a standalone project, it makes logical sense to put them in such a folder structure.
The project may be trying to be generic across many platforms. In this case it doesn't fit with MVC too well.

ASP.net MVC and modularity

I'm trying to find out if there is a way to use ASP.Net MVC to design a modular web application.
By modular, I mean that I should be able to drop a "package" (which could be made of a bunch of files, I don't necessarily require a single file deployment).
The idea is to deploy additional functionality seamlessly. Functionality could go from tweaking the existing web site (that is the easy part, any plugin architecture would do), up to having whole new site areas.
I'm looking for pointers as to
- if that is even possible
- what choices I have to make w/ regards to view engines for example
- any gotchas I should be aware of
I found one or two references, but ASP.Net MVC moves fast and they might be out of sync.
Any input is welcome (up to and including "don't go there") !
It's easy.
Start with creating a class library with the same structure as a regular MVC project. Make sure that all views are changed to "Embedded" in file properties.
Use an inversion of control container like Autofac and just tell it to register all controllers in all assemblies found in the current directory.
You need to create a custom VirtualPathProvider that looks for your views in all found plugin dlls. You might also want to make the VirtualPathProvider modify the views so that #inherits YouBaseView<TModelName> is added, since Views\Web.Config isn't used for embedded views.
I am thinking about doing something similar, I found this to be a good article to get started: http://sankarsan.wordpress.com/2009/04/12/a-layered-aspnet-mvc-application-part-iv/
What I have done so far is opt for the Castle Windsor IoC container - information about ASP .NET MVC and Windsor is here: http://stw.castleproject.org/Windsor.Windsor-tutorial-ASP-NET-MVC-3-application-To-be-Seen.ashx
I then use the Razor Generator from here: http://razorgenerator.codeplex.com/ so that I can compile views into separate assemblies.
And some code from here: https://github.com/csteeg/BoC/blob/master/Src/Commons.Web.PrecompiledViews to build a view engine that uses the IoC container.
With those two things and a few interfaces that are custom to my application I have been able to drop in "modules" by putting DLLs into a folder and have them appear as tabs within the hosting application.

Multiple web projects, (almost) same layout in Visual Studio

Im currently developing an application with two web projects (MVC):
Frontend
Backend
These two are going to have almost the same layout (few minor changes). Therefore I thought about creating a MasterPage, they can share. But since it's two projects in two different locations, where should I put the masterpage, images and javascript?
The images and javascript (static stuff), could be placed on a CDN.
But how 'bout the ASP.NET specific stuff? Any suggestions? Any help appreciated!
A MasterPage is a UserControl under the hood, so any method of sharing UserControls would work for MasterPages. Unfortunately this isn't that great of an experience in ASP.NET as the BuildManager (what compiles and connects the app paths to classes during build-time) only recognizes & processes files within the current project.
Sharing code-behind, or base classes is easy and works well. It is the design surface (.master, .ascx, .aspx) that doesn't have a lot of ability to be shared. You can use a deployment project to compile a web app project into an Assembly that can be referenced, but you will still need some design files in your actual project.
This might also be of help:
ScottGu: Creating and Using User Control Libraries
SO: How do I share user controls between web applications in ASP.NET?
It's possible to include an existing file from another project in a new project. You can do that. So just put it in one project and include it in the other.
Alternatively, you can create a 'core' library, and put things in there, but that doesn't 'play' well with controls and so on, so I'd recommend the first approach.

Can you convert an ASP.NET MVC Application to a Web Site using ASP.NET MVC and what problems might you run into?

I would like to use a WebSite project instead of a Web Application project for an MVC project.
What is the best way to accomplish this and are there signifigant problems that I might run into?
(as a side note, my reasoning for wanting this is because I have graphic designers who put files into SVN but they don't get added to the "project" and don't show up on deployment or deployment testing. My thought was that switching to a Web Site project might prevent this)
You don't need to do anything special if you are not using CodeBehind files (if you're using them, it'll be more complex but anyway, it's an MVC app. If you're using them, don't!). Just take an MVC Web app project and put all source files (*.cs) under App_Code directory of the Web site. That said, I fail to see any advantage for it.
The application project allows you to use the Models folder to embed classes into. This would then be compiled for that web project. A web site does not provide for this directly. It would require that you use an assembly project to maintain all of your classes. Rather than converting from one project type to another (which I am not sure how you wold go about doing that) you can simply attach an assembly project to your web application and not store any classes in the model folder of your application.
Having said this, you should keep your web project as an application as there usually are view specific classes that are required such as a your view model classes that belong in the web project.

Resources