Include Scripts from Feature folder in ScriptBundle - c#

I'm trying to include js files that are not part of the original ~/Scripts folder included in a MVC web project. Rather I have my script files inside of my Feature folder like: ~/Features/MyNewFeature/Scripts
However when running the app I get nothing but 404 errors when the request goes to find those js files. I'm not sure what I'm missing or if there's something that needs to be added to make this work. I know I've read that js files have to be in the Scripts folder but I'm having a hard time believing that since there's a way around not having your cshtml files inside a /Views folder.
bundles.Add(new ScriptBundle("~/bundles/mynewfeature")
.Include("~/Features/MyNewFeature/Scripts/Settings.js"));
Then to render in cshtml:
#Scripts.Render("~/bundles/mynewfeature")

So the problem was first starting once the/Views Web.config got moved to the Features folder. To fix the problem I was able to add a new handler that explicitly allows js files.
<system.webServer>
<handlers>
<remove name="BlockViewHandler" />
<add name="JavaScript" path="*.js" verb="GET, HEAD" type="System.Web.StaticFileHandler" />
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>

Related

ASP.NET- 404 when attempting to open PDF [duplicate]

I want to be able to request static .html files which are located in the ~/Views folder.
According to the documentation, the routing system checks to see if a URL matches a disk file before evaluating the application's routes.
But when I request the file a 404 error arises.
My file is located in ~/Views folder.
The URL is: http://[localhost]/Views/HtmlPage1.html
What have I missed?
I want to be able to request static .html files which are located in the '~/Views' folder.
You can't. There's a web.config file in this folder which explicitly forbids accessing any file from it. If you want to be able to access files from the client those files should not be placed in the Views folder which has a special meaning in ASP.NET MVC.
You could have a ~/Static folder where you could place your HTML files. And then access it like that:
http://example.com/yourapplicationname/static/foo.html
To allow files like js and html in Views folder edit the web.config in views-Folder:
<system.webServer>
<handlers>
<add name="JavaScriptHandler" path="*.js" verb="*"
preCondition="integratedMode" type="System.Web.StaticFileHandler" />
<add name="HtmlScriptHandler" path="*.html" verb="*"
preCondition="integratedMode" type="System.Web.StaticFileHandler" />
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
I want to be able to request static .html files which are located in
the ~/Views folder.
Well you can. The marked answer is not entirely correct, although it gives a solution.
The reasoning in the marked answer is correct, it is web.config (BlockViewHandler setting to be specific) in the Views folder that prevents the files to be accessed directly. It is there for securing the views in Asp.Net MVC. But if you asked a question about serving these files directly then you likely have a valid reason to do so, like using AngularJS partial views (as in our case) where we do not want to duplicate the views folder with weird names.
So here is a very simple tweak you can do in the web.config file found in the Views folder, without compromising security of your asp.net mvc views. This will secure the .cshtml files as usual but leave your .html files alone.A
Change this
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
--to--
<add name="BlockViewHandler" path="*.cshtml" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
Another alternate option is to insert an action method in any of the desired controller to serve the html file
public ActionResult MyHtml()
{
var result = new FilePathResult("~/Views/HtmlPage1.html", "text/html");
return result;
}
Access the html as http://yoursite/controller/MyHtml. You may extend this action method to accept the html file name as method/querystrign parameter and render the file at run time, e.g something like this.
public ActionResult MyHtml(string htmlPageName)
{
var result = new FilePathResult($"~/Views/{htmlPageName}.html", "text/html");
return result;
}
If you are planning to use inside view folder, above answers should be best but this answer may be useful for users who are migrating to asp.net mvc core. Placing files in wwwroot instead of views folder should make your html pages access easily as localhost/myfile.html
You can place it in the /Content folder.

ASP.NET Core page not found when publishing to IIS

I updated my ASP.NET Core project from RC1 to 1.0 RTM (with preview2 tooling), everything went fine. I can debug in Visual Studio without problems. Next I would like to upload the site on a Windows Server 2008 R2 and IIS 7.5. I installed the necessary Windows Hosting tools and created the web application in the IIS. The problem is, that after I tried to open the page, it returned a 404 error. As I see in the task manager, my application is running, but stops in listening mode.
In the log file I only see these entries:
Hosting environment: Production
Content root path: C:\inetpub\wwwroot\MySite
Now listening on: http://localhost:20706
Application started. Press Ctrl+C to shut down.
It seems like there is some problem with the IIS integration. My web.config file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="..\..\approot\MySite\MySite.exe" arguments="" forwardWindowsAuthToken="true" stdoutLogEnabled="true" />
<httpErrors errorMode="Detailed" />
</system.webServer>
</configuration>
I tried some workarounds from GitHub, which affected the Startup.cs file's Configure method without any success.
Any ideas?
FYI.. This web.config file was missing in the app folder.
This file should be at: C:\wwwwroot\myapp\
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath=".\XXX.exe" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" hostingModel="InProcess" />
</system.webServer>
</location>
</configuration>
Don't forget to replace XXX.exe with the correct name of your Web API exe name.
Ok, I had multiple problems, but the two main reasons why my site didn't work:
I cannot start the code from two separate places. I originally put the wwwroot content under the wwwroot folder (not so suprising), and the rest to the approot. Now everything should be in the same folder.
My release versions didn't want to work. I must use the contents of the debug folder. I don't know why.
And now, everything is ok.
please click on .cshtml file in Views folder and set build action=Content in right bottom panel in Visual Studio. My problem has been solved

Custom js httphandler in visual studio 2013

I'm trying wire up a custom IHttpHandler to process request to my website for *.js files. For the life of me I can't get my httphandler to even be created by IISExpress when a request is made for a js file to my web server. I've tried several variations of the system.webServer handlers setting including.
<add name="MyHandler"
type="BusinessLayer.Web_Stuff.MyHandler, BusinessLayer"
verb="*"
path="*.js"
resourceType="Unspecified"
preCondition="integratedMode"
modules="IsapiModule"
scriptProcessor="c:\windows\Microsoft.net\framework\v4.0.30319\aspnet_isapi.dll"/>
and
<add name="JSHandler"
type="BusinessLayer.Web_Stuff.MyHandler, BusinessLayer"
verb="GET,POST" path="*.js"
resourceType="Unspecified"
preCondition="integratedMode" />
I am running this in Visual Studio 2013 using IISExpress. If i change the path to *.test instead of js then this handler works without any problems, but i cant make it work for *.js files. It seems like IISExpress is handling those requests itself by just serving the files off of disk. Does anyone know how to bypass that so i can make my handler process requests for js files.
Correction: When i use the top web.config setting with IsapiModule specified, an instance of MyHandler is instantiated however the ProcessRequest(HttpContext context) method of the interface is never called on my object. With the bottom definition, no instances of my handler are ever instantiated.
After fighting with this for several hours i finally found the answer. The key was I had to add an attribute to the modules xml element in system.webServer
<modules runAllManagedModulesForAllRequests="true" >
Without this attribute, the process request handler was not being called. After setting that, this is all that was need in the handlers section.
<add name="JSHandler"
type="BusinessLayer.Web_Stuff.ScriptMinifyHandler, BusinessLayer"
verb="*"
path="*.js"
resourceType="Unspecified"
preCondition="integratedMode" />
I will have to read a bit more about this setting, but if anyone can tell me more about why that works I'd love to hear it.

Custom server control and Generic Web handler

I'm creating a custom server control which uploads files asynchronously to the server.
This solution uses flash element that posts the files to Generic Web handler aka ashx which then saves the posted file in a desired location.
It works grate, but with this approach I do need to create ashx file in each project, potentially, to handle the posts from the flash element.
What I would like to achieve is fully encapsulated server control that will use it's own ashx (or whatever can replace it) handler to upload the files.
Is it possible to do? Any ideas would be welcome.
Thanks.
You could include the handler within the class library containing the control and then only register it in web.config using the <httpHandlers> section:
<configuration>
<system.web>
<httpHandlers>
<add verb="*"
path="upload.ashx"
type="MyControl.MyUploadHandler.New, MyControl" />
</httpHandlers>
<system.web>
</configuration>
and if you are using IIS 7 Integrated pipeline mode to the <handlers> section:
<system.webServer>
<handlers>
<add name="UploadHandler"
verb="*"
path="upload.ashx"
type="MyControl.MyUploadHandler.New, MyControl" />
</handlers>
</system.webServer>
And if you are using ASP.NET 4.0 you could checkout the PreApplicationStartMethod infrastructure which allows you to dynamically register handlers:
[assembly: WebActivator.PreApplicationStartMethod(typeof(MyControl.StartUp), "PreApplicationStart")]

ASP.NET MVC Routing - add .html extension to routes

i am pretty new to MVC and Routing and i was asked to modify an app to use diffrent url's.
a task that is a bit over me since i have no experience.
ok, lets talk a bit of code:
routes.MapRoute(
"CategoryBySeName", // Route name
"products/{SeName}", // URL with parameters
new { controller = "Catalog", action = "CategoryBySeName" }
);
this works as expected, but then the client wanted ".html" at the end of paths, so i changed:
"products/{SeName}", // URL with parameters
to:
"products/{SeName}.html", // URL with parameters
which fails ( IIS 404 page - MapRequestHandler)
it seems like iis is trying to load a physical file with that name instead of passing it to the application.
Similar: ASP.NET MVC Routing to start at html page (not answered, Not duplicate)
You have to force all request through the ASP.NET pipeline, and you can do that by adding only this single line to the web.config of your application:
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
You're guess that an IIS handler is probably grabbing the request prior to MVC is likely correct.
Assuming IIS 7:
http://technet.microsoft.com/en-us/library/cc770990(v=ws.10).aspx
You need to edit the .html handler in IIS to use ASP.NET.
You can find it in the website properties under the home directory tab in app configuration in the mappings section in II6.
Something along the lines of (version may be different):
C:\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll is what you need to handle the .html files.
Changing the Application Pool from Classic to Integrated fixed the issue.
thank you guyz for your help.
Just add this section to Web.config, and all requests to the route/{*pathInfo} will be handled by the specified handler, even when there are dots in pathInfo. (taken from ServiceStack MVC Host Web.config example and this answer https://stackoverflow.com/a/12151501/801189)
<location path="route">
<system.web>
<httpHandlers>
<add path="*" type="System.Web.Handlers.TransferRequestHandler" verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" />
</httpHandlers>
</system.web>
<!-- Required for IIS 7.0 -->
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<add name="ApiURIs-ISAPI-Integrated-4.0" path="*" type="System.Web.Handlers.TransferRequestHandler" verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
</location>

Categories