【问题标题】:Link to .cshtml view from javascript从 javascript 链接到 .cshtml 视图
【发布时间】:2013-01-15 21:19:48
【问题描述】:

如何从 javascript 文件中直接指向 .cshtml 视图? 例如,为什么我不能将 .cshtml 视图与 angular.js 一起使用? 就像这个例子:

 .directive('encoder', ($timeout) => {
        return {
            restrict: 'E',
            transclude: true,
            scope: 'isolate',
            locals: { service: 'bind' },
            templateUrl: 'encoderTemplate.cshtml' // <-- that's not possible?
        }
    });

当然可以有一个返回任何你想要的动作方法,但我很好奇是否可以直接引用剃刀视图。

【问题讨论】:

  • 不,因为 IIS 不会提供 .cshtml 文件,因为它不会将其识别为静态文件。您可以考虑设置一个虚拟控制器,用于直接传递指定的 .cshtml 文件。
  • 请您回答...谢谢

标签: javascript asp.net-mvc angularjs angularjs-directive


【解决方案1】:

正如评论中提到的,您不能直接提供 .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 访问权限的内容(例如外部 .js 文件)之外拥有大部分 JavaScript 代码,您仍然可以利用 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();
    }
}

~/Views/Template/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")

Index.cshtml(或任何视图)

@* ... *@
@{ Html.RenderAction("Index", "Template"); }
@* ... *@

【讨论】:

  • 我将无法使用@Url.Action,因为代码来自位于单独 .js 文件中且未嵌入到剃刀视图中的 javascript
  • @Agzam:您还可以将全局设置存储在站点根目录中,然后在子文件中引用这些设置。 var SiteSettings = { templates: { encoder: '@Url.Action("Encoder","Template)' } } 然后templateUrl: SiteSettings.templates.encoder
【解决方案2】:

另一种选择是:

1.添加带有EncoderTemplate视图的TemplatesController

public class TemplatesController : Controller
{

     public ActionResult EncoderTemplate()
     {

           return View();
     }

}

2.将 Layout = null 添加到 EncoderTemplate.schtml 视图

@{
    Layout = null;
}

<div>Your html goes here</div>

3.指向如下图的EncoderTemplate.schtml

.directive('encoder', ($timeout) => {
    return {
        restrict: 'E',
        transclude: true,
        scope: 'isolate',
        locals: { service: 'bind' },
        templateUrl: '/Templates/EncoderTemplate' // you should not add .schtml
    }
});

【讨论】:

  • 这正是我所做的,它很有魅力。但是到目前为止,我还没有弄清楚如何在 Visual Studio 中使用 Resharper 作为运行器对 Jasmine 进行单元测试。有什么想法吗?
  • 对不起,我不知道。我没有使用 Resharper。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多