.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Does the Razor View Engine Support Generic Methods?

Posted By:      Posted Date: September 04, 2010    Points: 0   Category :ASP.Net
Trying out the Razor view engine and for some reason my generic helper methods are breaking. For example: public static class UrlHelperExtensions { public static string NonGenericHelper(this UrlHelper helper, Type controller) { return controller.Name; } public static string GenericHelper<TController>(this UrlHelper helper) { return typeof(TController).Name; } } Works as expected: @Url.NonGenericHelper(typeof(ProjectEuler.UI.Models.Home)) Breaks with the following exception: @Url.GenericHelper<ProjectEuler.UI.Models.Home>() "CS1502: The best overloaded method match for 'Microsoft.WebPages.WebPageUltimateBase.Write(Microsoft.WebPages.Helpers.HelperResult)' has some invalid arguments" The only workaround I could find is treating it as a "multi-token statement": @(Url.GenericHelper<ProjectEuler.UI.Models.Home>()) Not sure if this is the intended behavior or just a bug but it would be nice to be able to call a generic method as you would a non generic one.

View Complete Post

More Related Resource Links

Razor View Engine and Add Namespace in Web.Config Problem



I am working on a MVC project with Razor view engine and I have the following:


This only works if I have on the same view the following:

  @using SquishIt.Framework;

However, on my Web.Config I have the following:


      <!-- Namespaces -->
        <add namespace="System"/>
        <add namespace="System.Web.Mvc"/>
        <add namespace="System.Web.Mvc.Ajax"/>
        <add namespace="System.Web.Mvc.Html"/>
        <add namespace="System.Web.Routing"/>
        <add namespace="Microsoft.Web.Mvc"/>
        <add namespace="SquishIt.Framework"/>


So if "SquishIt.Framework" namespace is added on Web.Config why do I need to have the @using on the view?

Razor View Engine problem in Preview corrected in Beta?



On my projects I am using Razor View engine and I need to define custom Partial folders:

      // Define default view engine
      ViewEngines.Engines.Add(new CshtmlViewEngine());
      ViewEngines.Engines.Add(new WebFormViewEngine());

      // Define Razor view engine
      CshtmlViewEngine razor = (CshtmlViewEngine)ViewEngines.Engines[0];

      // Register partial views folders                
      razor.PartialViewLocationFormats = razor.PartialViewLocationFormats.Concat(new String[] { "~/Views/Partial/{0}.cshtml" }).ToArray();
      razor.AreaPartialViewLocationFormats = razor.AreaPartialViewLocationFormats.Concat(new String[] { "~/Areas/{1}/Views/Partial/{0}.cshtml" }).ToArray();

I had this because there was a problem in MVC 3 Preview. Was it corrected?

How should I register the Partial Views in MVC 3 Beta?

Thank You,


2 bugs in Razor view engine


I found 2 bugs in Razor view-engine.

1. I can't write any inline-templates in _ViewStart, cause

WriteLitralTo is not defined in ViewStartPage class

2. Translator inserts unnecessary whitespace writes

For ex:



Func<int, HelperResult> intFormatter = @<text>- @item -</text>;


That code translates to:

public class _ViewStart_cshtml : System.Web.Mvc.ViewStartPage {


public override void Execute() {

Func<int, HelperResult> intFormatter =item => new

System.Web.WebPages.HelperResult(__razor_template_writer => {

 WriteLiteralTo(@__razor_template_writer, " ");

 WriteLiteralTo(@__razor_template_writer, "- ");

                WriteTo(@__razor_template_writer, item);

 WriteLiteralTo(@__razor_template_writer, " -");






CS0103: The name 'WriteLiteralTo' does not exist in the current context

WriteLiteralTo is defined in System.Web.WebPages.WebPageBase, but

StartPage does not inherited from it.


Why these unnecessary WriteLiteralTo calls (" ")? - there is no in my

code. And these unneces

ASPX View Engine Vs Razor View Engine in ASP.NET MVC

View Engine in ASP.NET MVC is used to translate our views to HTML and then render to response.

Migrate an ASP.NET MVC 2 project to MVC 3 Razor view

Hi all, I have an application which has been built in MVC 2.0. I need to upgrade it to MVC 3.0 and that too by using Razor CSHTML engine. All the information which I got on the net was for MVC 3.0 Webform engine but nothing much for migrating to Razor. I would be very grateful if somebody shows me the steps or sample walkthrough for the purpose of this migration. Any help would be greatly appreciated. Thanks and regards, Tanay.

IE8 does not support explorer view on win 7 running moss


I hope MS has find the solution of this issue of IE8 not showing explorer view on MOSS sites . Its been long time now people are talking about this in blogs/online.

We  have MOSS farm running on win 2003 servers and now we are going to deploy win7 . In testing phase this issue is coming up and no solution yet. Can some one help with this . BTW I also tested this with beta IE9 it does not work .


MVC3 Razor - Create a instance of a View


In my controller method, I'm trying to create an instance of a View (cshtml file) before I wrap it around an ActionResult and return it.

Since there is no "class" for Razor Views to speak off, how does one go about creating an instance of a Razor View?

MVC3 beta Razor View Base class/inheritance issues


There seem to be a few issues in the way Razor views are designed and the code generated

1. Descendants of WebViewPage are forced to override Execute() (since it is abstract) but that method is never called. Why?

2. The virtual method ConfigurePage is never called in descendants that override this method.

3. CreatePageFromVirtualPath is no longer virtual. This was the one method that gave us access to the Layout Page (Master Page) instance. Now we no longer have this access. Why was this done?

How can one get access to the instance of a Layout page?

ASP.NET, HTML, JavaScript Snippet Support (VS 2010 and .NET 4.0 Series)

This post covers another useful improvement in VS 2010 - HTML/ASP.NET/JavaScript snippet support. Snippets allow you to be more productive within source view by allowing you to create chunks of code and markup that you can quickly apply and use in your application with a minimum of character typing.

Visual Studio has supported the concept of "snippets" for VB and C# in previous releases - but not for HTML, ASP.NET markup and JavaScript. With VS 2010 we now support snippets for these content types as well.

Multiple database support with Entity Framework

One of the features introduced in Entity Framework is being database independent. Which mean each database provider can support Entity Framework by implementing its provider.

This feature allows you build applications independent from the underplaying database provider. In this post I'm going to show how to build an application that support multiple databases using same conceptual model made by Entity Framework.

The Decorator Pattern & Extension Methods

The decorator pattern provides a formal way to add new functionality to an existing type, without sub-classing. First question: What is wrong with classical inheritance? In and of itself, nothing is wrong with the good old Is-A relationship. It is very common to derive a new class from an existing base class in order to override a few virtual members, and add a pinch of new functionality.

Truly Understanding View State

This article provides a detailed understanding of the ViewState.

GridView Parent Child View with JQuery Effects

I was just checked how to add some more cool effects to the GridView control using JQuery. In this post I will display parent-child data in the GridView control with some help of the wonderful JQuery library.

Let's first populate the GridView control.

Using jQuery to directly call ASP.NET AJAX page methods

Here I am looking to explain how to call page methods using jQuery. Using jQuery to directly call ASP.NET AJAX page methods

Creating a Generic Entity Framework 4.0 Repository

With .NET 4.0 right around the corner, I thought it would be cool to download Visual Studio 2010 beta 2 and start playing around with the next release of Entity Framework.

The initial release of Entity Framework came with a great deal of criticism. To make matters worse, there was a large uproar when it was rumored that Microsoft would be abandoning LINQ to SQL, in favor of Entity Framework. This was because, at the time, many developers felt Entity Framework was an inferior technology to LINQ to SQL. To set things right, Microsoft proactively moved forward with improving Entity Framework, in time for the 4.0 release of the .NET Framework. This is good news because my initial impressions, so far, have been nothing but positive.

A Generic Dialog ViewModel

I worked on a wpf viewmodel for dialogs, because it seemed strange to me that you needed for instance windows forms dialogs to open or save files in wpf. So I set about creating my own wpf dialogs. After creating the dialog viewmodels there still remained a small issue with the library and also I didn't have much time to write an article about it. I however published the library containing the viewmodels for dialogs with another article on my blog

Model View Controller (MVC) for ASP.NET

Model View Controller (MVC) architectures, immediately wonder how the MVC pattern can be applied in an ASP.NET world. This article will demonstrate how the MVC pattern can be applied to an ASP.NET web application and still maintain the basic Microsoft supplied architecture.
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend