English 中文(简体)
IgnoreRoute in ASP.MVC
原标题:

I am trying to access a .js file in the views directory. I have an MVC application with /Views/Home/MyControl.ascx I have a js file /Views/Home/MyControl.js

I wish to reference the .js file and keep it with the control. I have tried the following entries in the routing, and none seem to work.

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.IgnoreRoute("{file}.js");
        routes.IgnoreRoute("{resource}.js/{*pathInfo}");
        routes.IgnoreRoute("{controller}/{resource}.js/{*pathInfo}");
        routes.IgnoreRoute("{*alljs}", new { alljs = @".*.js(/.*)?" });

Please help, please don t suggest adding the .js file to the scripts directory. I would like to make it work this way, or know why it cannot be done.

I would put the script into the page, only script debugging is broken in VS2010 B2.

Thanks Regards Craig.

最佳回答

The Views folder is, well for views, and javascript should be put elsewhere. That s why the designers of the MVC framework put a web.config in this Views folder that denies access to any file inside. You could modify this defaut setting but be warned that this could be a potential security hole. So open the web.config file located in the Views folder and:

Replace:

<httpHandlers>
  <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
</httpHandlers>

with:

<httpHandlers>
  <add path="*.aspx" verb="*" type="System.Web.HttpNotFoundHandler"/>
  <add path="*.master" verb="*" type="System.Web.HttpNotFoundHandler"/>
  <add path="*.ascx" verb="*" type="System.Web.HttpNotFoundHandler"/>
</httpHandlers>

Navigate to http://yoursite/Views/test.js

P.S. You could also remove all the IgnoreRoutes you put in global.asax.

问题回答

Wouldn t this be a better solution using the DefaultHttpHandler for html resources and keeping the HttpNotFoundHandler for all other types of file

<httpHandlers>  
  <add path="*.html" verb="*" type="System.Web.DefaultHttpHandler"/> 
  <add path="*.*" verb="*" type="System.Web.HttpNotFoundHandler"/> 
</httpHandlers> 

Actually for IIS integrated mode, you need to use System.Web.StaticHttpHandler:

<httpHandlers>
  <add path="*.css" verb="*" type="System.Web.StaticHttpHandler"/> 
  <add path="*.js" verb="*" type="System.Web.StaticHttpHandler"/> 
  <add path="*.*" verb="*" type="System.Web.HttpNotFoundHandler"/> 
</httpHandlers>

Apparently System.Web.DefaultHttpHandler works in IIS classic mode only.

In MVC 4 I had to update the handler section as well as the httpHandlers section.

I updated the web.config in the Views folder with the following.

<httpHandlers>
  <add path="*.aspx" verb="*" type="System.Web.HttpNotFoundHandler"/>
  <add path="*.cshtml" verb="*" type="System.Web.HttpNotFoundHandler"/>
</httpHandlers>

<handlers>
  <remove name="BlockViewHandler" />
  <add name="BlockViewHandlerRazor" path="*.cshtml" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
  <add name="BlockViewHandlerAspx" path="*.aspx" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签