Friday, March 21, 2014

Durandal.js: Enable Razor views

By default when you use the Durandal MVC template, your views should be created as plain html files.

But what if you want to use ASP.NET MVC Razor files(.cshtml) instead? This is perfectly possible but requires some configuration work from you:

First, let’s switch the Durandal view extension from .html(the default) to .cshtml. Open up the main.js file and set the viewExtension property on the viewEngine object:

The next step is to create a generic controller that will serve .cshtml files:

And of course, don’t forget to add a route that matches this controller:

As a last step copy the web.config file from Views/web.config to /App/views/web.config (so Razor views work in this location):

image

    4 comments:

    Michael Schreiber said...

    Do you have the visual studio solution to this? I am trying to get it to work, but when I use the URL http://localhost:58933/App/views/test.cshtml
    OR
    http://localhost:58933/App/views/test

    I get 404 The resource cannot be found.


    Anonymous said...

    i get 404 too

    Bart Wullems said...

    I'll see if I can post a complete solution...

    hutenmy cawodson said...

    It is not necessarily common to obtain very good extravagance solutions pertaining to this budget friendly selling price, nevertheless view business has created a lot of historically decades the fact that nowadays looks after really are now not available exclusively for the successful small number of. You can receive any superior check and not spend unbelievably huge costs http://www.hotreplicaonline.com. While you knowledge celebrities continually prefer to jump out and turn different? Very well, the truth that consequently many keep working into Breitling pieces should really inform you precisely how good they are just.