ASP.Net MVC3: place .js files next to the View folder instead of Scripts

We would like to share javascript with our Razor views (so that we can test). Can we find .js files next to the views that they correspond to, and not in the Scripts folder? For example, we would like to see this in the solution browser:

MyMvcProject - Views - Home - About.cshtml - About.js 

However, I do not know the links to the .js file from the .cshtml view.

+6
source share
3 answers

For security reasons, asp.net-mvc blocks all files from accessing the / Views folder from the URL. This can be circumvented, but I would suggest NOT to do this for security reasons.

Usually you leave your scripts in the Scripts folder, especially system-wide ones like jquery and unobtrusive stuff. Thus, they can be easily updated through NuGet as new versions or patches are released.

I am not sure why you have a problem with testing and leave them at the default location.

+9
source

If you are talking about breaking "user" scripts from your view and putting them in a separate folder for testing your javascript, then yes, you can put them in a folder of your choice.

However, I would not recommend placing them next to your view ..., which will create a dirty project structure and make it much harder if you ever want to minimize your javascript.

I generally leave the framework files in the framework in the Scripts folder for the reasons that Mystery Man mentioned in my answer, however I put the "user" or related javascript files within /Content/js/ .

To link to them, you simply added a link to your view or main page (layout):

 <script type="text/javascript" src="@Url.Content("~/Content/js/somelink.js")"></script> 
+2
source

Open views /Web.config and replace it

 <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" /> 

with this

 <add name="ExcludeRazorViews" path="*.cshtml" verb="*" type="System.Web.HttpNotFoundHandler" /> 
+2
source

Source: https://habr.com/ru/post/908939/


All Articles