ASP.NET MVC 4 Bundles giving 404 error - asp.net

I want to add Bundles into an existing ASP.NET MVC 4 (.NET 4.5) website that uses:
Umbraco 6.1.5
Microsoft ASP.NET Web Optimization Framework 1.1.3 (https://www.nuget.org/packages/microsoft.aspnet.web.optimization/)
I attempted to follow these directions: https://gist.github.com/jkarsrud/5143239, and the CSS loaded fine before I started down the bundles path.
On page load it inserts the style reference:
<link href="/bundles/marketingcss" rel="stylesheet">
But a 404 error happens:
> GET http://localhost:20459/bundles/marketingcss 404 (Not Found)
Here's what I've in code:
Web.Config
<add key="umbracoReservedPaths" value="~/umbraco,~/install/,~/bundles" />
Global.asax
<%# Application Codebehind="Global.asax.cs" Inherits="MapCom.Global" Language="C#" %>
Global.asax.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Optimization;
using System.Web.Security;
using System.Web.SessionState;
using Umbraco.Web;
namespace MapCom
{
public class Global : UmbracoApplication
{
protected void Application_Start(object sender, EventArgs e)
{
base.OnApplicationStarted(sender, e);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
}
_Layout.cshtml
#Styles.Render("~/bundles/marketingcss");
BundleConfig.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Optimization;
namespace MapCom
{
public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
BundleTable.EnableOptimizations = true;
///
///Marketing Site CSS Bundle
///
bundles.Add(new StyleBundle("~/bundles/marketingcss")
.Include("~/plugins/bootstrap/css/bootstrap.min.css")
.Include("~/css/font-awesome.min.css")
.Include("~/plugins/parallax-slider/css/parallax-slider.css")
.Include("~/css/combinedStyles.min.css")
.Include("~/plugins/ladda-buttons/css/ladda.min.css")
.Include("~/plugins/ladda-buttons/css/custom-lada-btn.css"));
}
}
}
Any ideas?

After much digging into Umbraco, I noticed that the project I'm working on is utilizing a class:
public class ApplicationEventHandler : IApplicationEventHandler
Which is explained in more detail here: http://our.umbraco.org/documentation/Reference/Events/application-startup
But the gist is:
In order to bind to certain events in the Umbraco application you need to make these registrations during application startup. Based on the Umbraco version you are using there are various ways to hook in to the application starting. The higher the version you are using the more robust this becomes.
So Umbraco's overriding some of the ASP.NET start-up methods.
I solved my issue by adding a reference to System.Web.Optimization in ApplicationEventHandler.cs and moved my bundle registration to this method in that class:
public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}

The ONLY thing that worked for me here was adding the path to the bundle to the following setting in web.config
My bundle Code
BundleTable.Bundles.Add(new ScriptBundle("~/opt/jscripts").Include(
"~/Scripts/twitterFetcher.js"
));
BundleTable.EnableOptimizations = true;
Umbraco default setting
<add key="umbracoReservedPaths" value="~/umbraco,~/install/" />
New setting
<add key="umbracoReservedPaths" value="~/umbraco,~/install/,~/opt/" />

try the following suggestions:
1.Do not use keyword bundles for your css virtual path so just change it to something else, and use the keyword bundles for your scripts only.
like this:
bundles.Add(new StyleBundle("~/Content/marketingcss")
(Remember you need to make sure that if you have a file called marketingcss under content folder you should change the virtual path to something else, so the virtual path shouldn't duplicate the physical path)
2.in your _Layout.cshtml use the following line instead of yours:
#Styles.Render(BundleTable.Bundles.ResolveBundleUrl("~/Content/marketingcss"))
3.if the above suggestions didn't work then try this one in your _Layout.cshtml instead:
<link href="#System.Web.Optimization.BundleTable.
Bundles.ResolveBundleUrl("~/Content/marketingcss")"
rel="stylesheet"
type="text/css" />
updated
4.just saw one of our friend's comment about not to putting .min files in your bundle, which is correct so you must not include .min files in your budleConfig class, and just use the normal files like bootstarp.css instead of bootstrap.min.css and do the same for all other files, of course make sure you have the normal files first so not just change the names.
I hope it helps to fix your code

Related

asp.net web forms URL routing doesn't map to the physical file

I have an asp.net web form website in which i use URL routing
the thing is when ever I try ti navigate to a matched route e.g "http://localhost:51878/brand/adidas" it doesn't open the specified aspx file it shows HTTP Error 404.0 - Not Found and navigates to D:\Websites\Website\brand\adidas
update: I tried adding break points to the global.asax.cs file apparently it doesn't even go through the Application_Start function
here's my
global.asax`
<%# Application Language="C#" CodeBehind="Global.asax.cs" %>
and here's my global.asax.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.Security;
using System.Web.SessionState;
using System.Data;
using System.Data.SqlClient;
public partial class Global : HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
protected void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("brandsRoute", "brand/{brand}", "~/brand.aspx");
}
}
would u please tell me what I'm doing wrong
The code you posted looks fine, except for the erroneous > character under your using statements, which may be causing your code not to build, and perhaps you are running a last good build version that doesn't include your new route.

Load ScriptResourceDefinition JQuery on every page

This has been asked before, but nobody responded, so I ask again as I feel it is important.
Unobtrusive validation for web forms works great, but, only when validation controls are added to a form. For other pages that lack validation controls, no reference to the JQuery file is rendered.
I currently use JQuery on all pages, so reference it manually in a Master page file,
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
The problem is that when I access a page with my own JQuery logic and validation controls, then two references get created (my own, plus ASP.Net's ScriptResourceDefinition).
How can I achieve one of the following:
Let ScriptResourceDefinition know that the file exists and has already been added?
Force ScriptResourceDefinition to render JQuery on every page regardless of whether it detects validation controls?
Found the answer here https://stackoverflow.com/a/12628170/792888
The answer is to inherit from ScriptManager and stop ASP.Net's built-in behaviour of creating the unnecessary (duplicate) JQuery link
using System;
using System.Linq;
using System.Web.UI;
namespace WebApplication46
{
public class CustomScriptManager : ScriptManager
{
protected override void OnInit(EventArgs e)
{
Page.PreRenderComplete += Page_PreRenderComplete;
base.OnInit(e);
}
private void Page_PreRenderComplete(object sender, EventArgs e)
{
var jqueryReferences = Scripts.Where(s => s.Name.Equals("jquery", StringComparison.OrdinalIgnoreCase)).ToList();
if (jqueryReferences.Count > 0)
{
// Remove the jquery references as we're rendering it manually in the master page <head>
foreach (var reference in jqueryReferences)
{
Scripts.Remove(reference);
}
}
}
}
}
In web.config this is wired up to replace the standard ScriptManager:
<system.web>
<pages>
<tagMapping>
<add tagType="System.Web.UI.ScriptManager" mappedTagType="WebApplication46.CustomScriptManager" />
</tagMapping>
</pages>
</system.web>

css minified needed in development stage?

For readability I created my stylesheets like this:
div.icons,div.sizes,div.configs
{
width:213px;
float:left;
}
Is it necessary to use this style for performance reasons:
div.icons,div.sizes,div.configs{width:213px;float:left;}
I am using ASP.NET webforms. Can I still use the bundling functionality from ASP.NET MVC for this?
No, you don't need to style it like that for performance reasons. Write it for readability. Bundling and minification will handle the performance part.
Yes, bundling and minification works in Webforms. Here are key steps from the post Adding Bundling and Minification to Web Forms:
Create your bundle:
using System.Web.Optimization;
public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
//...
Register your bundle: (global.asax)
void Application_Start(object sender, EventArgs e)
{
BundleConfig.RegisterBundles(BundleTable.Bundles);
//...
Reference your bundle: (masterpage)
<asp:PlaceHolder runat="server">
<%: Scripts.Render("~/bundles/jquery") %>
</asp:PlaceHolder>
Get bundling and minification from NuGet.
I would not write css or JavaScript with that in mind. Instead I would rely on a minifier to do the job. That way your css files are readable.

ASP.NET and dynamic Pages

Is it possible to write a System.Web.UI.Page and stored in an assembly?
And how can I make iis call that page?
So I will go deeply...
I'm writing a class like that:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Runtime.InteropServices;
using System.Reflection;
using WRCSDK;
using System.IO;
public partial class _Test : System.Web.UI.Page
{
public _Test()
{
this.AppRelativeVirtualPath = "~/WRC/test.aspx";
}
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("You are very lucky!!!");
}
}
That are stored into an assembly.
So Now How can I register that assemply and obtain that http://localhost/test.aspx invoke that class?
Thanks.
Bye.
You'll want to use an HttpHandler or HttpModule to do this.
Registering the assembly is just like registering any assembly -- just define that class in a code file and have the compiled DLL in your bin directory.
Then, as an example, you can create a IHttpHandlerFactory:
public class MyHandlerFactory : IHttpHandlerFactory
{
public IHttpHandler GetHandler(HttpContext context, ........)
{
// This is saying, "if they requested this URL, use this Page class to render it"
if (context.Request.AppRelativeCurrentExecutionFilePath.ToUpper() == "~/WRC/TEST.ASPX")
{
return new MyProject.Code._Test();
}
else
{
//other urls can do other things
}
}
.....
}
Your web.config will include something like this in the httpHandlers section
<add verb="POST,GET,HEAD" path="WRC/*" type="MyProject.Code.MyHandlerFactory, MyProject"/>
Not sure what you're after here. If you set up a deployment project, there's a setting to have it merge all the dll files into a single assembly. Is that what you want? Either way, if you want to reuse the same code behind class for several aspx pages, it is the page declarative (1st line of code in the aspx) that you must change.
Few options
1. You can refer to this assembly as part of visual studio references
2. Use relfection to load the assembly and class from your test ASAPX page.

How can I copy the DynamicData templates from another site?

I'm attempting to start using DynamicData functionality in a previously existing website. Basically I'm following this tutorial. When I got to the part about creating the Field Templates I decided I could probably create a new site with the Dynamic Data stuff built in, and then just copy the folder over.
Unfortunately when I do that and try to compile I get the error "Could not load type..." for just about every .ascx file in the DynamicData directory. I named the "new" project the same as the pre-existing site so that the namespace would be the same... but I can't think of what else I could be missing.
Everything looks ok, except that the *.ascx.Designer.cs files are showing inside the Solution Explorer. I tried deleting one and then copying that file back into just the directory but it didn't work. I'm assuming I need to do something special with those so that Visual studio handles them properly and can compile?
Here is one of the .aspx files:
<%# Control Language="C#" CodeBehind="FilterUserControl.ascx.cs" Inherits="EService.FilterUserControl" %>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"
EnableViewState="true" ontextchanged="new">
<asp:ListItem Text="All" Value="" />
</asp:DropDownList>
Here is the matching .cs file:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Web.DynamicData;
namespace EService
{
public partial class FilterUserControl : System.Web.DynamicData.FilterUserControlBase
{
public event EventHandler SelectedIndexChanged
{
add
{
DropDownList1.SelectedIndexChanged += value;
}
remove
{
DropDownList1.SelectedIndexChanged -= value;
}
}
public override string SelectedValue
{
get
{
return DropDownList1.SelectedValue;
}
}
protected void Page_Init(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
PopulateListControl(DropDownList1);
// Set the initial value if there is one
if (!String.IsNullOrEmpty(InitialValue))
DropDownList1.SelectedValue = InitialValue;
}
}
}
}
And here is the .ascx.designer.cs file:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.1433
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace EService
{
public partial class FilterUserControl
{
protected System.Web.UI.WebControls.DropDownList DropDownList1;
}
}
EDIT: If I create the file in the website, then copy the contents from the temporary site I created it seems to compile just fine. Really have no idea what the issue is here... I tried manually modifying files to match the results of copying and they still wouldn't work unless I actually created them in the site. It's just bizarre...
The problem was due to the temporary site creating the <%# Control > definitions with the CodeBehind property instead of the CodeFile property. For some reason pages in the actual website will only compile when CodeFile is declared...
DD will work with Either Website or Web Application. however when copying in from an other project the types need to be the same otherwise it's lot of editing to convert swee my article here Getting Dynamic Data Futures filters working in a File Based Website.

Resources