admin管理员组文章数量:1405329
I'd like to auto-generate some JavaScript files, with proper .js
extensions in their URLs, using ASP.NET MVC 5.
Here's my problem;
I'm using require.js throughout my site and it's working pretty well. However, not all of my JavaScript files are real files on disk. Some of them must be generated at runtime. So I've written a controller which generates the dynamic JavaScript files, and serves them when using the default routing;
// ~/Resource/CommonRes -- from ResourceController.CommonRes()
define([], function() {
return {
"menuItemConfiguration":"Configuration",
"menuItemAdmin":"Admin",
"manageAccount":"Manage Account",
"logOff":"Log off"
...
};
});
However, I need to have the route available as ~/Scripts/Resources/CommonRes.js
-- the .js
extension is vital since I'm actually returning a require.js module, to be invoked like this;
require(['Resources/CommonRes'], function(monRes) {
// code the uses the resource file
});
And in this case, the module named Resources/CommonRes
will always be looked for at ~/Scripts/Resources/CommonRes.js
. Hence the need to serve it up with that extension.
I can't seem to get the routing correct. I've tried the following but to no avail;
routes.MapRoute(
name: "Resource Scripts",
url: "Scripts/Resource/{action}",
defaults: new { controller = "Resource" },
namespaces: new string[] { "AITrackRecord.Controllers" }
);
I'd like to auto-generate some JavaScript files, with proper .js
extensions in their URLs, using ASP.NET MVC 5.
Here's my problem;
I'm using require.js throughout my site and it's working pretty well. However, not all of my JavaScript files are real files on disk. Some of them must be generated at runtime. So I've written a controller which generates the dynamic JavaScript files, and serves them when using the default routing;
// ~/Resource/CommonRes -- from ResourceController.CommonRes()
define([], function() {
return {
"menuItemConfiguration":"Configuration",
"menuItemAdmin":"Admin",
"manageAccount":"Manage Account",
"logOff":"Log off"
...
};
});
However, I need to have the route available as ~/Scripts/Resources/CommonRes.js
-- the .js
extension is vital since I'm actually returning a require.js module, to be invoked like this;
require(['Resources/CommonRes'], function(monRes) {
// code the uses the resource file
});
And in this case, the module named Resources/CommonRes
will always be looked for at ~/Scripts/Resources/CommonRes.js
. Hence the need to serve it up with that extension.
I can't seem to get the routing correct. I've tried the following but to no avail;
routes.MapRoute(
name: "Resource Scripts",
url: "Scripts/Resource/{action}",
defaults: new { controller = "Resource" },
namespaces: new string[] { "AITrackRecord.Controllers" }
);
Share
Improve this question
asked Sep 22, 2014 at 23:14
Steve CooperSteve Cooper
21.5k17 gold badges74 silver badges90 bronze badges
1 Answer
Reset to default 9According to Darin Dimitrov,
IIS intercepts the request because it contains a file extension and hijacks it thinking it is a static file and not passing it to your application.
Add this to your Web.config
:
<system.webServer>
<handlers>
<add name="ScriptsHandler" path="Scripts/Resource/*.js" verb="GET"
type="System.Web.Handlers.TransferRequestHandler" />
</handlers>
</system.webServer>
Then, the route:
routes.MapRoute(
name: "DynamicJavascript",
url: "Scripts/Resource/{action}.js",
defaults: new { controller = "Resource" }
);
Sample Controller:
public class ResourceController : Controller
{
public ActionResult MyScript()
{
return Content("var greeting = \"Hello World!\";");
}
}
Result:
本文标签: aspnet mvcCreate a MVC route ending quotjsquot and returning JavaScriptStack Overflow
版权声明:本文标题:asp.net mvc - Create a MVC route ending ".js" and returning JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744899524a2631262.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论