正如评论中提到的,您不能直接提供 .cshtml 文件,但是,如果您选择,可以使用控制器来呈现内容:
public class TemplateController : Controller
{
// create a ~/Views/Template/Encoder.cshtml file
public PartialViewResult Encoder()
{
return PartialView();
}
}
然后像你一样引用它@Url.Action
:
{
....
templateUrl: '@Url.Action("Encoder", "Template")'
}
来自评论
如果您拥有 Razor 访问权限之外的大部分 JavaScript 代码(例如外部 .js 文件),您仍然可以利用 Url 构建器,只需稍稍不同即可。例如,我可能会这样做:
public class TemplateController : Controller
{
// Add a child method to the templates controller that outputs default
// configuration settings (and, since it's a child action, we can re-use it)
[ChildActionOnly]
public PartialViewResult Index()
{
// You could build a dynamic IEnumerable<ConfigRef> model
// here and pass it off, but I'm just going to stick with a static view
return PartialView();
}
}
〜/视图/模板/Index.cshtml
<script type="text/javascript">
if (typeof window.App === 'undefined'){
window.App = {};
}
App.Templates = {
Encoder: '@Url.Action("Encoder", "Template")',
Template1: '@Url.Action("Template1", "Template")',
Template2: '@Url.Action("Template2", "Template")'
};
</script>
@*
the template files would then reference `App.Templates.Encoder`
when they need access to that template.
*@
@Scripts.Render("~/js/templating")
索引.cshtml(或任何观点)
@* ... *@
@{ Html.RenderAction("Index", "Template"); }
@* ... *@