简体   繁体   English

如何在Durandal中使用.cshtml和.html文件

[英]How can I use .cshtml and .html files with Durandal

I started using DurandalJs framework with asp.net MVC. 我开始在asp.net MVC中使用DurandalJs框架。 It's working perfect. 工作正常。

But now I need to use .cshtml files as views for durandal. 但是现在我需要将.cshtml文件用作durandal的视图。 So I added to root web.config 所以我添加到根web.config

<add key="webpages:Enabled" value="true" /> 

But DurandalJs still try to get .html files as views. 但是DurandalJs仍然尝试将.html文件作为视图。

So I correct viewEngine.js file: 所以我纠正了viewEngine.js文件:

    return {
    viewExtension: '.cshtml',
    viewPlugin: 'text',

but now DurandalJs required that all views files should have ".cshtml" extension. 但现在DurandalJs要求所有视图文件都应具有“ .cshtml”扩展名。

So could I use ".html" and ".cshtml" files together? 那我可以一起使用“ .html”和“ .cshtml”文件吗?

In main.js I added row: 在main.js中,我添加了以下行:

viewLocator.useConvention('viewmodels', 'ViewsProxy/GetView?name=', 'ViewsProxy/GetView?name=');

And implemented ViewsProxyController like below: 并实现了ViewsProxyController,如下所示:

public class ViewsProxyController : Controller
{
    public ActionResult GetView(string name)
    {
        string viewRelativePath = GetRelativePath(name);
        string viewAbsolutePath = HttpContext.Server.MapPath(viewRelativePath);

        if (!System.IO.File.Exists(viewAbsolutePath))
        {
            viewAbsolutePath = ReplaceHtmlWithCshtml(viewAbsolutePath);
            if (System.IO.File.Exists(viewAbsolutePath))
            {
                System.Web.HttpContext.Current.Cache.SetIsHtmlView(name, false);
                viewRelativePath = ReplaceHtmlWithCshtml(viewRelativePath);
                return PartialView(viewRelativePath);
            }
            throw new FileNotFoundException();
        }

        FilePathResult filePathResult = new FilePathResult(viewAbsolutePath, "text/html");
        return filePathResult;
    }

    private string ReplaceHtmlWithCshtml(string path)
    {
        string result = Regex.Replace(path, @"\.html$", ".cshtml");
        return result;
    }

    private string GetRelativePath(string name)
    {
        string result = string.Format("{0}{1}", AppConfigManager.DurandalViewsFolder, name);
        return result;
    }
}

So now durandal application can work with .html and .cshtml. 因此,durandal应用程序现在可以与.html和.cshtml一起使用。

You need to change your routes to accept cshtml as the extensions for MVC routes. 您需要更改路由以接受cshtml作为MVC路由的扩展名。

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}.cshtml",
    defaults: new { controller = "Home", action = "Index", namespaces: new string[] { "PAWS.Web.Controllers" }
);

Also you need to make sure your application pool is running under integrated and not classic. 您还需要确保您的应用程序池在集成而不是经典的环境下运行。

But I don't recommend doing this. 但我不建议这样做。 You should try and not have the server render any of your HTML. 您应该尝试不要让服务器呈现任何HTML。 The reason for this is explained here 这样做的原因进行了说明这里

Take a look at Durandal viewEngine and viewLocator customizations that allows to use both local '.html' and remote '.cshtml' views simultaneously. 看一看Durandal viewEngineviewLocator自定义viewEngineviewLocator自定义项允许同时使用本地'.html'和远程'.cshtml'视图。

Is it possible to have multiple viewEngine.viewExtension 是否可以有多个viewEngine.viewExtension

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM