MVC4 bundling route exclusion - c#

I make use of the bundling features in MVC4 by calling bundles.EnableDefaultBundles();, this allows me to browse to http://website.com/content/css which outputs a singular file of all the CSS files in the /content directory - great.
The issue is I have the following route which loads a blog post from the DB by title: /post/{anything} and in this case {anything} is css (for arguments sake, I can't change it) so the bundling is getting confused and trying to bundling everything in the post directory, which doesn't exist.
Is there any way to exclude a particular URL format or route from being bundled? I think not calling EnableDefaultBundles() would work but does that mean I would have to create bundles for everything manually?

Are you using an old version of the Optimization package? EnableDefaultBundles was removed prior to 1.0. You can still accomplish the equivalent of that method by adding the equivalent js/css DynamicFolderBundles.

Related

CSS file updates don't apply in ASP .NET CORE

I have a CSS file for my homepage, it's just a simple transparent background image.
When I change something in that file, it does not apply the changes, older version is compiled.
Even if delete that file completely from computer, it still loads it (I saw that within inspect element).
My view has just link to that CSS file and nothing else. It has defined Layout page also.
It's happening to me all the time. If I create some other as source, it works but I get the same problem for that new file also after the initial version.
What could itbe ? I'm fairly new in ASP .NET CORE but it looks like it's storing those files in a server. One time I accidentally opened console in inspect element and got some bad connection error and it worked from that point on.
Sounds like your browser is caching your css file. A common way to force it to use the updated version is to add the attribute asp-append-version="true" to all your link and script tags so that your browser always fetches the most up to date version.

MVCSiteMapProvider won't use Razor Templates for Menu

I'm enjoying using the well-made MVCSiteMapProvider through Nuget at the moment but I've hit a roadblock.
I'm trying to modify the template for the #Html.MvcSiteMap().Menu() helper. The file I'm modifying is ..\Views\Shared\DisplayTemplates\MenuHelperModel.cshtml and no matter what change I make to the file, the template does not update when rendered.
I've done a Find All with notepad++ and found that within MvcSiteMapProvider.dll there is two templates, one for classic asp.net and one for Razor. So clearly the template within the .dll file is being called - but how do I make this not the case?
It may be worth knowing that although the templates are located in ..\Views\Shared\DisplayTemplates\, the site is configured to use a different folder for Views.
edit: also if I pass in a template name to the helper, still no effect.
Templated helpers are an MVC creation, not one of MvcSiteMapProvider. I strongly suspect your issue is due to reconfiguring your Views folder. MVC does not support a way to reconfigure your templates folder.
But as pointed out in the accepted answer, it is possible to put them under ~/Views/CurrentController/DisplayTemplates/. You might be able to use that feature to put the templates in your newly configured folders. This means that you would need to add a copy of the templates for every single controller, though.
The best solution is not to change the MVC folders from their defaults.
If that is not an option, you might consider rolling your own Menu HTML helper based on the code from the current one that returns an HtmlString rather than using templates.
The templated helpers are better because you can edit the code after it is deployed (which is why we did it that way), but it comes with the caveat that you have to rely on MVC's default folder structure to use them.

Versioning CSS and Javascript files automatically in Umbraco 7

I'm new to both .NET development and Umbraco due to recently starting a new role where I will be working heavily with both. As usual before starting development I am looking for some shortcut or helper functions that will enable me to work more productively. One set of helper functions I have found I have included below:
public static string DatedContent(this UrlHelper urlHelper, string contentPath)
{
var datedPath = new StringBuilder(contentPath);
datedPath.AppendFormat("{0}m={1}",
contentPath.IndexOf('?') >= 0 ? '&' : '?',
getModifiedDate(contentPath));
return urlHelper.Content(datedPath.ToString());
}
private static string getModifiedDate(string contentPath)
{
return System.IO.File.GetLastWriteTime(HostingEnvironment.MapPath(contentPath)).ToString("yyyyMMddhhmmss");
}
The functions above are called as follows:
<script src="#Url.DatedContent("~/scripts/product.js")"></script>
The aim of these functions is to automatically version Javascript and CSS files to ensure that the latest versions of each JavaScript and CSS file are pulled through by appending a modified date query string to the end of the file references.
My issue here is, and I apologise that this is such a newbie question, but where do I put this function in Umbraco 7 or is this functionality already supported?
From what I have learnt so far, content pages all have some base properties that include date created and date modified however CSS and Script files do not have these properties so I cannot simply reference these object properties using inline Razor code and condense them with the ToString method as I am doing the functions above.
My first thought was that these functions could be included at the top of the template where the razor code block is included automatically but as I am new to .Net I don't know if this is a standard convention.
Any help would be greatly appreciated.
This is not supported out of the box. Except if you use "Client Depencency" framework. This is a sort of "bundels" framework, similar to the Asp.net framework.
What you do is perfectly possible. Umbraco is still .Net.
2 ways of getting this code in your site:
make a new project, build a DLL and drop the DLL in your umbraco website
put your code in App_code and let IIS build it.
Note: you are checking on EVERY request the modifydate. If you have a busy website, you will want to optimize this. E.g. use a partial using #Html.CachedPartial(...) or using a Macro

Can I have a folder in my ASP.NET MVC4 project that is a webforms project?

I tried googling, I promise! I may not be asking the question the right way. We have an existing project that is webforms (.NET 3.5 I think). It's not really a VS solution, just a folder with this structure:
/
../App_Code
../bin
../pages
../global.asax
../this.html
../that.aspx
../web.config
In the "pages" folder is where we have a big ugly mess of .aspx pages and there code behind.
App_Code holds some helper classes and whatnot. They rest should be self explanatory.
Questions:
What is the best strategy to put this mess inside an mvc4 application?
What do I have to do with routing back and forth (i.e. from .cshtml pages to .aspx and back again)
Any other considerations?
Yes, you can do it. I would recommend adding this to your routeConfig:
routes.IgnoreRoute("pages/{*pathInfo}");
It may not be even absolutely necessary, but it'll keep the request from even attempting to be parsed out in the routecollection. Just incase you have a page and a route rule that can collide. We do this for our webservices which reside in our MVC 4 application (inherited from an older website project).
That will just work.
You can mix and match any kind of ASP.Net stuff in one project.
The ASPX files will be accessible using their actual paths, just like pure WebForms project.
You can also call MapPageRoute() to apply routing to those files.

How to minify and combine Javascript and CSS

Can someone give me pointers on how I can minify and combine my CSS as the site I'm working on has performance issues. Also, are external JS and CSS libraries also minified and combined or left as is?
There are several convenient options for .NET, for example:
Chirpy
SquishIt
Cassette
And new in ASP.NET 4.5:
Bundling in ASP.NET 4.5
I'd recommend reading Google's Page Speed and their Closure Compiler Service.
I would also add RequestReduce for .net options. It may be the only runtime bundling option for minifying external resources. Its unique from the other .net options in that it grabs the unminified content via http instead of via the file system. It can also sprite your background images.
I know this is an old question but I thought I will give a better answer (trick).
In visual studio if you do format code (Ctrl + K + D) then css will be formatted nicely for you.

Categories