Migrate to Razor - asp.net-mvc

Is it possible to migrate an existing ASP.NET MVC3 project using the standard (webforms-like) template engine to Razor? How?
Thanks

It should be possible.
Rename your *.aspx to *.cshtml. Firstpay attention to control directives, for example
Inherits="System.Web.Mvc.ViewPage<MvcApplication1.Models.TheModel>
now becomes
#model MvcApplication1.Models.TheModel
Now examine your views carefully and replace all tags to razor-style tags (<% %> -> #)
Note that you don't have to migrate all your views - it's perfectly legal to mix the two view engines in a single MVC application so you can plan your migration over time.

Related

When should we use Html Helpers, Razor Helpers or Partial Views?

These three different features exist in the Razor view engine and can be used to achieve similar results. In the end all three of them just render pieces of HTML code, but the way to define and use them is fairly different. I know that:
Html Helpers are created as extension methods for the HtmlHelper class. They frequently use the TagBuilder class to generate some HTML and always should return an IHtmlString.
Razor Helpers (#helper methods) can be defined locally (in the same razor file) or globally (in the App_Code directory). They are small snippets of HTML code that can be reused exclusively in Razor files.
And finally, Partial Views are just regular view files that can be included in other view files using the #Html.Partial helper.
My question is:
Is there a specific scenario for each one of these features? Or it comes down to different flavors to achieve the same result?
HTML Helpers are for reusable components. e.g. WebGrid, Pager, etc. These are distributed as assemblies and have no dependency on Razor. Choose this if:
Functionality is truly reusable and applicable to any application
You don't want people to modify it, or want to version it
Partials Views are a way to split large views into smaller parts to keep things more manageable. They are also useful for reusability that is specific to your application. These are located by the view engine, so you can have the same partial defined in different places (e.g. Views/Shared), allowing you to customize per controller, area or display mode. Choose this if:
Functionality is application-specific
Want to customize per controller, area or display mode
Local Helpers are a way to execute the same template many times, without having to repeat yourself. You can also use it to break views into parts to avoid deep nesting, but keeping everything in the same file. Choose this if:
Functionality is view-specific
Application Helpers (in App_Code) are a mix between local helpers and HTML helpers. Choose this if:
Prefer Razor over TagBuilder
Don't mind distributing files instead of assemblies
Prefer type-safe method-call syntax instead of #Html.Partial(name)

Why do I need an underscore for partial views in asp.net mvc

Just to distinguish between a view used inside a dialog or used in a foreach loop (customer details) ?
You don't need an underscore. It's just a convention, and MVC is very keen on using conventions.
Mike Brind has put this nicely in the question Why does Razor _layout.cshtml have a leading underscore in file name?:
Since layout pages in Web Pages are not intended to be served directly, they are prefixed with the underscore. And the Web Pages framework has been configured not to allow files with leading underscores in their names from being requested directly.
Besides that, I find it very helpful to use this convention to differentiate between full views and partial ones.
#Marius Schulz gives a nice reference, but then misses the point. Yes, the underscore helps to differentiate between full views and partial ones, but more importantly, it prevents partial views from being loaded directly by their URL, which could provide some potentially ugly results! (Like no css, for starters.)
EDIT: Mystere Man is right...what was I thinking? URLs in MVC point to controller/action, not to view.
Also, it is possible to mess things up and display a partial in a seperate window, so the naming convention does not prevent that. #Marius Schulz and I had the same misinterpretation of his quote.
The leading underscore is a useful convention to differentiate full and partial views, and I will continue to use it, but is is just a convention, not a functional difference.

Share a razor declarative helper between multiple mvc web projects

Let's say I have 2 MVC web projects (web1 and web2) and 1 project containing shared views (common) (using the razorgenerator of David Ebbo)
web1 and web2 both have a test.cshtml file. Several blocks of code in both test.cshtml files are exactly the same.
I'm trying to find out if it's possible to share a declarative helper (#helper) between several cshtml files which are in DIFFERENT projects. So putting a cshtml file in my App_Code does not help (I would need to have 1 in each web project, which is obviously not what I want).
I know I could create a bunch of shared partial views in my 'common' project, but it seems kinda overhead to create 20 cshtml files that each contains a very small portion of html.
I know I can create a standard helper method (static string GenerateAPieceOfHtml(this HtmlHelper helper, ....)), but there I loose the ease of writing html as you can do it in a cshtml file.
For a short while I thought I bumped into an answer that would allow me to do it. But as I wrote in a comment, that code did not compile for me.
I hope my question is clear :)
[Update]
As csharpsi asks in a comment.. I did try out the code from the other post, but it did not spit out any HTML for me. Since I started to think that that answer should probably do the trick since it has 13 upvotes, I decided to give it a second try..
Again I didn't get any output, but then I tried it a little bit different.. and success!
I was trying this (which doesn't spit out any html on the page):
#{ new Test().DoSomething(Model); }
This is the version that DOES WORK:
#{
var html = new Test().DoSomething(Model);
#html
}
Other version that works:
#(new Test().DoSomething(Model))
What should I do with this question? Delete it? Write an answer myself?
Why are you trying to use razor helper for this anyway ? Razor helpers are one-particular-viewengine hack, your application shouldnt rely on them on many places (even amongst different websites). In this case, be sure to use standard MVC way - HTML helper. These you can easily share between websites, for example you can make your own class library full of them.

How do I customize view templates in ASP.NET MVC using Visual Studio 2010?

When I add a new strongly-typed view in VStudio 2010, I get fields layed out like this.
<div class="display-label">Id</div>
<div class="display-field"><%= Html.Encode(Model.Id) %></div>
In 2008, it used to use p tags instead of all these divs, my css is set up to handle the p tags properly, but since upgrading I have these now, so I have to spend time editing by hand removing the divs and adding p tags. (Plus, I just like the idea of using logical p tags instead of this mess.)
Is there a way I can modify the template?
Yes, you can. This if for MVC3 but I believe it is very similar if not the same for MVC2.
You can also override the templates by creating your own display templates. Brad Wilson shows this... and one more....

MVCContrib input builder vs MVC 2 editor templates

Is there any comparison? Pros and cons?
MVC 2 editor template is an evolution of MVCContrib input builder.
It's true that is missing a few things, like Darin Dimitrov said, but It's a fact that it's has a stronger architecture.
Pros:
Have a special folder EditorTemplates under the shared folder AND under the view! (area supported too). In the older MVCContrib input builder you must put the templates in the a shared folder. In order to use it with a special folder you must add a new view engine that will look on this folder.
Cons:
None.
Pros of input builders: they can recognize attributes on the model such as MaximumStringLength and set the maxlength html attribute. They also can recognize the presence of the Required attribute and put the required class on the element.
Pros of editor templates: support of incremental sequencing.

Resources