Javascript bundling in another folder - asp.net-mvc

How can I have JavaScript bundling working from another folder (aside from the Script folder). If I do this:
bundles.Add(new ScriptBundle("~/bundles/search").Include("~/Views/Search/*.js"));
The browser tells me the javascript file can't be found. Is it possible to do this
or do all my sripts have to be in the Scripts folder?
Basically I want my Javascript included in my View subfolders

You need to change web.config in Views folder according this answer:
In ASP.NET MVC, how can I load script from my view folder?
Good example from Ashley Lee:
<system.webServer>
<handlers>
<add name="JavascriptViewHandler" path="*.js" verb="*"
preCondition="integratedMode" type="System.Web.StaticFileHandler" />
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*"
preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>

Since you specifically want to only include javascript files, make the following change to your ~/Views/web.config file, by adding the "JavascriptViewHandler" section.
<system.webServer>
<handlers>
<add name="JavascriptViewHandler" path="*.js" verb="*"
preCondition="integratedMode" type="System.Web.StaticFileHandler" />
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*"
preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
This will preserve all of the current blocking for non-javascript files.

I don't know if i understood your question properly, but if you want to use a script file from any folder in a View or preferably in it's Layout, you can add the following tag in <head> section of you View or _Layout.cshtml:
<script src="#Url.Content("~/Scripts/jquery-ui.min.js")" type="text/javascript"></script>
You can mention the complete path to your script file instead of ~/Scripts/jquery-ui.min.js

Related

Asp.Net Mvc Cloud hosting

Asp Net MVC project works fine in IIS Server ,But after uploading it to a cloud space ,it shows an error code 500.19 ,I put the database in cloud ,it also works fine with IIS
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<directoryBrowse enabled="false" />
<handlers accessPolicy="Read, Script">
<add name="PHP via FastCGI" path="*.php" verb="*" modules="FastCgiModule" scriptProcessor="C:\PHP\php-cgi.exe|-d open_basedir=C:" resourceType="Unspecified" requireAccess="Script" />
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified"/>
</handlers>
<aspNetCore processPath="dotnet"
arguments=".\TickLogicApplication.dll"
stdoutLogEnabled="false"
stdoutLogFile=".\logs\stdout"
hostingModel="inprocess"/>
<httpErrors errorMode="Detailed" existingResponse="PassThrough" />
</system.webServer>
</configuration>
and in my local project .Can some one give me a solution?

MVC5 routing error (404.0) error on IIS 7.5

An old story of everything works fine on the development machine but not on the host server.
HTTP Error 404.0 - Not Found
Module IIS Web Core
Notification MapRequestHandler
Handler StaticFile
Error Code 0x80070002
Trying to produce the same error on the development machine by adding <remove name="UrlRoutingModule-4.0" /> to system.webServer - modules and resolving it when adding <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />, but that not works on the host. I've also try <modules runAllManagedModulesForAllRequests="true" /> but nothing changed.
And here is the handlers part:
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<remove name="WebDAV" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
<add name="MiniProfiler" path="mini-profiler-resources/*" verb="*" type="System.Web.Routing.UrlRoutingModule" resourceType="Unspecified" preCondition="integratedMode" />
</handlers>
Few more information:
MVC.Net 5.2.2.0
IIS 7.5 on Windows 2008
Integrated .Net 4.0 application pool.
Is there anything else that I should do?
Now, having the same problem in IIS8, any idea?
I had similar problem. It turns out to be discrepancy with Virtual folder name and the URL I was constructing to call Web API route:
var URLGetUserAccesses = "/api/Login/GetUserAccesses";
Vs.
var URLGetUserAccesses = "../api/Login/GetUserAccesses";

asp mvc IIS7 less/sass empty file

I'm trying to use less in my new project. I've installed dotLess and System.Web.Optimization.Less packeges (as described in Yet Another "Add LESS to your ASP.NET MVC Project" Post) and locally everything works fine. But when I publish project IIS7 server responds with empty css file /Content/Main/site?v=
Here is my web.config
...
<configSections>
<section name="dotless" type="dotless.Core.configuration.DotlessConfigurationSectionHandler, dotless.Core" />
</configSections>
<system.web>
<httpHandlers>
<add path="*.less" verb="GET" type="dotless.Core.LessCssHttpHandler, dotless.Core" />
</httpHandlers>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
<add name="dotless" path="*.LESS" verb="*" type="dotless.Core.LessCssHttpHandler,dotless.Core" resourceType="File" preCondition=""/>
</handlers>
</system.webServer>
<dotless minifyCss="false" cache="true" web="false" />
And my BundleConfig.cs
bundles.Add(new LessBundle("~/Content/Main/site").Include("~/Content/Main/Site.less"));
When I append css extension to my Site.less file (Site.less.css) server responds with content (/Content/Main/site?v=K-FFpFNtIXPUlQamnX3qHX_A5r7TM2xbAgcuEmpm3O41) but it still pure less with all its variables and etc.
The same with Bundle Transformer: Sass and SCSS
What have I missed?
As you are getting
/Content/Main/site?v=K-FFpFNtIXPUlQamnX3qHX_A5r7TM2xbAgcuEmpm3O41
then this means that the bundling is working correctly. Therefore there is probably something in Site.less that is causing the problem. One thing to watch out for is relative urls. For example if you have an image in your css
.bacon {
background-image: url(bacon.png);
}
The bundle will look for this with ~/Content/Main taken from the name of the bundle. (/site is fine and can be anything, it's just the name of the bundle)
The first thing to try is take out any imports, images etc and see if it bundles correctly. You can do this on your local dev build by enabling optimizations in the your bundle config.
BundleTable.EnableOptimizations = true
Another way to solve this is to remove dependency on System.Web.Optimization.Less and dotLess and use free Visual Studio Extension https://github.com/madskristensen/WebCompiler for precompilation of less to css (with and without minification).

Adding HttpHandler to MVC application

I have an MVC4 application to which I added an HttpHandler:
<system.web>
...
<httpHandlers>
<add path="Files" verb="*" type="MyHttpHandler" />
</httpHandlers>
</system.web>
I also ignored the relevant path in RegisterRoutes so that the requests to "Files" are not handled by MVC:
routes.IgnoreRoute("Files/{*pathInfo}");
The problem is that the MyHttpHandler is invoked only for requests to "Files", not for any of its children or sub-folders.
I've tried using the <location> element, but getting that to work means that you will be hard coding the application's virtual path in the "path" attribute (e.g., <location path='MyApp\Files'>).
What is the correct method to use to allow all requests for "Files" and any of its sub-folders (and sub-folders of those folder, etc) to get routed to MyHttpHandler?
Scratch that...<location> seems to work OK. Though, you need both the <web> and <webServer> entries to ensure that it works for both IIS and the Visual Studio Development Server. For example:
<location path="Files">`
<system.webServer>
<handlers>
<add name="MyHandler" path="*" verb="*" type="MyHttpHandler" />
</handlers>
</system.webServer>
<system.web>
<httpHandlers>
<add path="*" verb="*" type="MyHttpHandler" />
</httpHandlers>
</system.web>
</location>

Mvc Mini Profiler requests for files with and without RouteBasePath

This is driving me crazy, I'm testing my app with both Firefox and IE, on Firefox works just fine, but on IE mini profiling is making these 5 requests
/Content/mvc-mini-profiler/mini-profiler-includes.css?v=1.9.0.0
/Content/mvc-mini-profiler/mini-profiler-includes.js?v=1.9.0.0
/Content/mvc-mini-profiler/mini-profiler-jquery.tmpl.beta1.js
/mini-profiler-includes.css?v=1.9.0.0
/mini-profiler-includes.js?v=1.9.0.0
I put the RouteBasePath to be ~/Content/mvc-mini-profiler but for some reason is also making requests without it.
Am I doing something wrong or is this some kind of bug?
Thanks.
For the last two
/mini-profiler-includes.css?v=1.9.0.0
/mini-profiler-includes.js?v=1.9.0.0
View the source and make sure you don't have any script or link elements with those urls in the src or href attributes. That would make a request to the server without your base url.
Did u added below code in you web config?? if not then add and try it
<system.webServer>
...
<handlers>
<add name="MiniProfiler" path="mini-profiler-resources/*" verb="*" type="System.Web.Routing.UrlRoutingModule" resourceType="Unspecified" preCondition="integratedMode" />
</handlers>
</system.webServer>
and also you can try with
<system.webServer>
<handlers>
<add name="UrlRoutingModule1" path="mini-profiler*.js" verb="*" type="System.Web.Routing.UrlRoutingModule" resourceType="Unspecified" preCondition="integratedMode" />
<add name="UrlRoutingModule2" path="mini-profiler*.css" verb="*" type="System.Web.Routing.UrlRoutingModule" resourceType="Unspecified" preCondition="integratedMode" />
<add name="UrlRoutingModule3" path="mini-profiler*.tmpl" verb="*" type="System.Web.Routing.UrlRoutingModule" resourceType="Unspecified" preCondition="integratedMode" />
</handlers>
</system.webServer>

Resources