13

I got the DurandalJS StarterKit template on VS2012... All works great...

But in some views I need to do something like that:

@if (Roles.IsUserInRole("Administrators"))
{
   <p>Test</p>
}

However with durandal all my views are '.html' files... Is that possible to use '.cshtml' files to access some information like that?

Or is there any other way to do that with durandal?

Junior

Junior
  • 154
  • 1
  • 1
  • 8
  • 4
    Please read this post for information on why using cshtml might be a bad idea: http://stackoverflow.com/questions/15554919/how-to-use-partial-views-with-durandal-js-mvc3 – Evan Larsen Mar 23 '13 at 17:21
  • Actually, I wouldn't necessarily be that concerned about the points at the above link, which deals more with MVC than just Razor. Using a little cshtml to manage the security and bundling (which is what we do) has no impact at all on the SPA nature of your app. It's still just one page, sent one time from the server. Thereafter, it's all Durandal. – drdwilcox Sep 05 '13 at 21:17
  • @Junior Could you please tell me how did you managed to work with mvc and .cshtml view in durandal - all the steps? – KKS Jan 19 '14 at 21:36

8 Answers8

32

I am doing it like this:

  1. Create a generic controller for Durandal views:

    public class DurandalViewController : Controller
    {
      //
      // GET: /App/views/{viewName}.html
      [HttpGet]
      public ActionResult Get(string viewName)
      {
        return View("~/App/views/" + viewName + ".cshtml");
      }
    }
    
  2. Register a route:

    routes.MapRoute(
        name: "Durandal App Views",
        url: "App/views/{viewName}.html",
        defaults: new { controller = "DurandalView", action = "Get" }
    );
    
  3. Copy Views/web.config to /App/views/web.config (so Razor views work in this location).

This lets me use the normal Durandal conventions (even the html extension for views), and put durandal views as cshtml files in their normal location without adding any more server code.

If you also have static html views, you can also place the cshtml views in a subfolder or use the normal MVC /Views folder.

Maarten
  • 1,941
  • 1
  • 15
  • 14
  • I actually like this much better than my own answer; much cleaner. – Young Mar 26 '13 at 23:58
  • Perfect! Long time was looking for such a clean answer. – mehrandvd Sep 15 '13 at 10:18
  • @Maarten have you got any sample that I can download ? Also, did you found any other option to use .cshtml with durandaljs ? Plz reply. – KKS Jan 19 '14 at 21:49
  • Thanks, I'm using similar code, but it uses AttributeRouting. Please check out link [here](http://www.codeproject.com/Articles/715041/Durandal-View-s-with-Razor-and-Csharp-ViewModel) – Nilesh Thakkar Mar 14 '14 at 13:00
  • This solution works fantastic for the Views and ViewModels convention. Do you have a suggestion for how to extend it with js and chtml files organized into features? – Bill Shihara Jan 29 '16 at 05:08
  • Here is my specific question: http://stackoverflow.com/questions/35056736/how-do-you-configure-pathing-in-durandal – Bill Shihara Jan 29 '16 at 05:21
8

I wouldn't recommend using ASP.NET MVC with Durandal.

What you are probably looking to do is use the Razor view engine (to get the benefits of a compiler, strong typing etc.) which exists independently from ASP.NET MVC. Just WebAPI for data I/O is more than enough to very efficiently create a Durandal.js application.

If you are interested in using Razor/CSHTML with Durandal and Knockout there is an open source option out there called FluentKnockoutHelpers that may be exactly what you are looking for. It offers much of the 'nice' parts of ASP.NET MVC allowing you to use the awesome abilities of Durandal and Knockout with almost no downfalls.

In a nutshell it provides a bunch of features which makes doing Durandal/Knockout development just as easy as ASP.NET MVC. (You simply provide a C# type that your JavaScript model is based off of for most of the features.) You only have to write JavaScript and un-compiled markup for complicated cases which is unavoidable and no different than MVC! (Except in MVC your code would also likely end up would also be a big jQuery mess which is why you are using Durandal/Knockout in the first place!)

Features:

  • Painlessly generate Knockout syntax with strongly typed, fluent, lambda expression helpers similar to ASP.NET MVC
  • Rich intellisense and compiler support for syntax generation
  • Fluent syntax makes it a breeze to create custom helpers or extend whats built in
  • OSS alternative to ASP.NET MVC helpers: feel free to add optional features that everyone in the community can use
  • Painlessly provides validation based on .NET types and DataAnnotations in a few lines of code for all current/future application types and changes
  • Client side JavaScript object factory (based on C# types) to create new items in for example, a list, with zero headaches or server traffic

Example without FluentKnockoutHelpers

<div class="control-group">
    <label for="FirstName" class="control-label">
        First Name
    </label>
    <div class="controls">
        <input type="text" data-bind="value: person.FirstName" id="FirstName" />
    </div>
</div>
<div class="control-group">
    <label for="LastName" class="control-label">
        Last Name
    </label>
    <div class="controls">
        <input type="text" data-bind="value: person.LastName" id="LastName" />
    </div>
</div>
<h2>
    Hello,
    <!-- ko text: person.FirstName --><!-- /ko -->
    <!-- ko text: person.LastName --><!-- /ko -->
</h2>

Provide FluentKnockoutHelpers with a .NET type and you can do this in style with Intellisense and a compiler in Razor / CSHTML

@{
  var person = this.KnockoutHelperForType<Person>("person", true);
}

<div class="control-group">
    @person.LabelFor(x => x.FirstName).Class("control-label")
    <div class="controls">
        @person.BoundTextBoxFor(x => x.FirstName)
    </div>
</div>
<div class="control-group">
    @person.LabelFor(x => x.LastName).Class("control-label")
    <div class="controls">
        @person.BoundTextBoxFor(x => x.LastName)
    </div>
</div>
<h2>
    Hello,
    @person.BoundTextFor(x => x.FirstName)
    @person.BoundTextFor(x => x.LastName)
</h2>

Take a look at the Source or Live Demo for an exhaustive overview of FluentKnockoutHelper's features in a non-trivial Durandal.js application.

John Culviner
  • 22,235
  • 6
  • 55
  • 51
7

Yes, you can absolutely use cshtml files with Durandal and take advantage of Razor on the server. I assume that also means you want MVC, so you can do that too and use its routing.

If you don;t want the routing then you can set the webpages.Enabled in the web.config, as the other comments suggest.

<add key="webpages:Enabled" value="true" /> 
John Papa
  • 21,880
  • 4
  • 61
  • 60
  • durandal work out of the box with cshtml? Could you please mention steps to how to use cshtml and thus, a controller directly for viewlocator in durandal? – KKS Jan 19 '14 at 21:35
  • @John-papa can you explain it more please.I tries in web,config but durandal gives me error "View Not Found. Searched for "views/shell" via path "text!views/shell.html".". [I have changed shell.html to shell.cshtml] – ebrahim.mr Mar 03 '16 at 21:57
5

I don't recommend that you use .cshtml files as views directly. You're better off placing the .cshtml files behind a controller.

For example, take the HotTowel sample, edit /App/main.js, and replace the function definition with the following:

define(['durandal/app', 
        'durandal/viewLocator', 
        'durandal/system',
        'durandal/plugins/router',
        'durandal/viewEngine', 
        'services/logger'],
function (app, viewLocator, system, router, viewEngine, logger) {

Note that we added a reference to the Durandal viewEngine. Then we need to replace

viewLocator.useConvention();

with

viewLocator.useConvention('viewmodels', '../../dynamic'); 
viewEngine.viewExtension = '/';

The first argument to viewLocation.useConvention sets the /Apps/viewmodels/ directory as the location for the view models js files, but for the view location, uses the URL http://example.com/dynamic/, with an extension of '/'. So that if Durandal is looking for the view named 'shell', it will reference http://example.com/dynamic/shell/ (this is because the view directory is mapped relative to the viewmodels directory, hence /App/viewmodels/../../dynamic will give you simply /dynamic).

By convention, this previous URL (http://example.com/dynamic/shell/) will be mapped to the controller DynamicController, and the action "Shell".

After this, you simply add a controller - DynamicController.cs, like this:

// will render dynamic views for Durandal
public class DynamicController : Controller
{
    public ActionResult Shell()
    {
        return View();
    }

    public ActionResult Home()
    {
        return View();
    }

    public ActionResult Nav()
    {
        return View();
    }

    public ActionResult Details()
    {
        return View();
    }

    public ActionResult Sessions()
    {
        return View();
    }

    public ActionResult Footer()
    {
        return View();
    }
}

Create .cshtml files for each of the above actions. This way you get to use controllers, server side IoC et al to generate dynamic views for your SPA.

Young
  • 442
  • 5
  • 14
  • what if we want multiple folders for views?! ... for example one folder for movies , one for products and ...?! – amir-yeketaz Mar 21 '13 at 09:40
  • You can always use Controller.Redirect to point to a completely different .cshtml view in different folders. – Young Mar 21 '13 at 09:53
  • thanx . so you say we have to have one controller and mutiple views for that? – amir-yeketaz Mar 21 '13 at 10:04
  • This is great, as I've been looking for a concrete example on how to do this. However I share @Young 's concerns in that durandal seems to make it difficult (or not obvious on how) to use the MVC conventions we've grown accustomed to. Even the HotTowel SPA template deviates from these, and considering it is labelled as an MVC template, it doesn't feel like MVC once past the initial page load! – Brett Postin Mar 21 '13 at 10:23
  • 1
    I have posted a question [here](http://stackoverflow.com/q/15545705/295813) with further questions raised from this answer. – Brett Postin Mar 21 '13 at 11:15
2

DurandaljS is a client framework which forms mainly a solid base for single-page apps (SPA). I assume you are using asp.net web API as your server technology. In that case, you can determine the user's role inside your API controller and based on that return data to the client. On the client you can use Knockout "if" binding in order to show / hide certain areas of your page.

What you perhaps can do is placing this code in the Index.cshtml.

S P
  • 4,615
  • 2
  • 18
  • 31
2

Following link shows how to customize moduleid to viewid mapping

http://durandaljs.com/documentation/View-Location/

by convention durandal tries to find view url in following steps

1) Checke whether object has getView() function which returns either dom or a string ( url for the view)

2) If object does not have getView function then checks whether object has viewUrl property

3) If above two steps fails to produce url or a DOM view drundal falls to default convention

which maps moduleid xyz.js to view xyz.html using view url ( path of Views folder ) defined in main.js

so for moduleid xyz.js path of the view will be views/xyz.html

you can overwrite this default mapping behavior by overwriting convertModuleIdToViewId function.

So there are many ways you can customize your view url for specific model (.js object)

N30
  • 3,463
  • 4
  • 34
  • 43
  • Finally. SO many people asking how, and only you seem to answer the question instead of lecturing about how the question is wrong and we shouldn't be using server-side and client-side stuff at the same time. – Neil Barnwell Aug 29 '16 at 19:08
1

I made an extension to Durandal which gives you the ability to place an applicationContent div in your cshtml file together with the applicationHost div. In applicationContent you can now use both ASP .Net MVC syntax together with knockout bindings.

Only thing I did was put some extra code in the viewLocator.js file which looks for an applicationContent div:

        locateViewForObject: function(obj, area, elementsToSearch) {
        var view;

        if (obj.getView) {
            view = obj.getView();
            if (view) {
                return this.locateView(view, area, elementsToSearch);
            }
        }

        if (obj.viewUrl) {
            return this.locateView(obj.viewUrl, area, elementsToSearch);
        }

        view = document.getElementById('applicationContent');
        if (view) {
            return this.locateView(view, area, elementsToSearch);
        }

        var id = system.getModuleId(obj);
        if (id) {
            return this.locateView(this.convertModuleIdToViewId(id), area, elementsToSearch);
        }

        return this.locateView(this.determineFallbackViewId(obj), area, elementsToSearch);
    },

Your original cshtml file can now do something like this:

<div class="row underheader" id="applicationContent">
<div class="small-5 columns">
    <div class="contentbox">
        @using (Html.BeginForm("Generate", "Barcode", FormMethod.Post, Attributes.Create()
                                                                                    .With("data-bind", "submit: generateBarcodes")))
        {
            <div class="row formrow">
                <label for="aantalBijlagen">@Translations.Label_AantalBijlagen</label>
            </div>
            <div class="row">
                <select name="aantalBijlagen" class="small-6 columns">
                    <option>0</option>
                    <option>1</option>
                    <option>2</option>
                    <option>3</option>
                    <option>4</option>
                </select>
            </div>
            <div class="row">
                <button class="button right" type="submit" id="loginbutton"><span class="glyphicon glyphicon-cog"></span> @Translations.Action_Generate</button>
            </div>
        }
    </div>
</div>
<div class="small-7 columns" data-bind="if: hasPdfUrl">
    <div class="contentbox lastcontent">
        <iframe data-bind="attr: {src: pdf_url}"></iframe>
    </div>
</div>

You can find my fork of the durandal project here and a small blogpost of what and how I did this here.

GitteTitter
  • 434
  • 5
  • 7
0

I'm not very familiar with DurandalJS but because it's a client-side system, it should make no difference what technology is used on the server to generate the HTML markup. So if you use Razor CSHTML files to generate the HTML on the server, DurandalJS should work just fine with it.

If you're getting a particular error then please share that error, but I can't think of any reason why it wouldn't work.

Eilon
  • 25,582
  • 3
  • 84
  • 102
  • 1
    similar question is here : https://groups.google.com/forum/#!topic/durandaljs/hpfZB-WFEXk – amir-yeketaz Feb 20 '13 at 19:13
  • Thanks... I tried that (viewEngine.viewExtension = '.cshtml'), but got the same error pointed there (403 Forbidden) – Junior Feb 20 '13 at 19:17
  • @Junior You don't want to request the CSHTML file directly - you can't run an MVC view on its own. Instead, request the MVC controller action that will run the view. For example, /Home/Index in an MVC app will run the Index() action method on the HomeController class. That will then render the ~/Views/Home/Index.cshtml view. – Eilon Feb 20 '13 at 19:36
  • 1
    @Junior Also, set webpages:Enabled to true in web.config – amir-yeketaz Feb 22 '13 at 11:08
  • 2
    @Junior, please don't use the webpages:Enabled key -- see my full answer above. – Young Mar 21 '13 at 04:17