MVC - Accessing css, image, js files in view folder

后端 未结 4 1822
抹茶落季
抹茶落季 2020-12-11 15:00

In my mvc 3 application, assuming I have a folder structure like \\Views\\Account\\js\\custom.js How do I add that file to my view in the Account\\index.c

相关标签:
4条回答
  • 2020-12-11 15:21

    The web.config file in the /Views folder restricts all access to files in the folder by default:

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

    You could change that, but it's probably more secure overall to not store the assets in the views folder.

    0 讨论(0)
  • 2020-12-11 15:22

    You can use a UrlHelper:

    <script src="@Url.Content("~/view/account/js/custom.js")" type="text/javascript"></script>
    
    0 讨论(0)
  • 2020-12-11 15:31

    Add this under your views web.config Handlers section

    <add name="JavaScriptHandler" path="*.js" verb="*" preCondition="integratedMode" type="System.Web.StaticFileHandler" />
    
    0 讨论(0)
  • 2020-12-11 15:38

    The best way is to use T4MVC - http://mvccontrib.codeplex.com/wikipage?title=T4MVC No need to use magic strings...

    0 讨论(0)
提交回复
热议问题