0

I started using DurandalJs framework with asp.net MVC. It's working perfect.

But now I need to use .cshtml files as views for durandal. So I added to root web.config

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

But DurandalJs still try to get .html files as views.

So I correct viewEngine.js file:

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

but now DurandalJs required that all views files should have ".cshtml" extension.

So could I use ".html" and ".cshtml" files together?

pryabov
  • 702
  • 2
  • 7
  • 23

3 Answers3

2

In main.js I added row:

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

And implemented ViewsProxyController like below:

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.

pryabov
  • 702
  • 2
  • 7
  • 23
1

You need to change your routes to accept cshtml as the extensions for MVC routes.

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. The reason for this is explained here

Community
  • 1
  • 1
Evan Larsen
  • 9,935
  • 4
  • 46
  • 60
0

Take a look at Durandal viewEngine and viewLocator customizations that allows to use both local '.html' and remote '.cshtml' views simultaneously.

Is it possible to have multiple viewEngine.viewExtension

Community
  • 1
  • 1
irium
  • 691
  • 8
  • 14