从asp.net mvc控制器返回一个js文件

str*_*oir 6 asp.net-mvc controller return file

大家好,程序员

我想为我的应用程序中的每个MVC设置一个单独的js文件

/Controllers/
    -TestController.cs
/Models/
/Views/
    /Test/
        -Index.aspx
        -script.js
Run Code Online (Sandbox Code Playgroud)

而且我想将js包含在Index.aspx中

<script type="text/javascript" src="<%=UriHelper.GetBaseUrl()%>/Test/Js"></script>
Run Code Online (Sandbox Code Playgroud)

或者更容易,当我在浏览器中调用http:// localhost/Test/Js时,它会显示script.js文件

如何在Controller中编写Action?我知道这必须用返回File方法完成,但我还没有成功创建方法:(

Laz*_*rus 15

怎么样:

    public FileContentResult Js()
    {
        return File(System.IO.File.ReadAllBytes(Server.MapPath("/Test/script.js")), "text/javascript");
    }
Run Code Online (Sandbox Code Playgroud)

我将补充说你真的应该把你的脚本放在你的Content文件夹下,因为MVC完全是关于上面的约定配置,并试图用你的视图放置脚本你违反惯例.