English 中文(简体)
Can we use Razor on an existing ASP.NET 4 website?
原标题:

Is it possible to use Razor on an existing ASP.NET 4 website?

最佳回答

You shouldn t even need to open the site in Web Matrix if you already have VS2010 and MVC 3 (which includes the Visual Studio tools for building ASP.NET Razor websites) installed. Installing MVC 3 makes the libraries required for developing Razor pages available, even to existing web applications.

See:

http://www.asp.net/webmatrix/tutorials/program-asp-net-web-pages-in-visual-studio

You don t need to create a new Web Pages site (as per the instructions). You can just open up an existing web site, right click the site s root folder, click add item and you should see "Web Page (Razor)" as an option.

Inellisense and debugging works in the Razor pages just like the Web Forms pages

As stated above, keep in mind that ASP.Net Web Pages (Razor) and ASP.Net WebForms are really different platforms, and the reusable components of each can not (or at least should not) be used

问题回答

Yes, you can use Razor with an existing ASP.NET WebSite. Simply open your website using the WebMatrix tool and start adding CSHTML files. One caveat is that if your website is using WebForms controls the WebMatrix tool will not provide any help working with them in existing aspx pages. Additionally, Razor does not support WebForms so you will not be able to add something like <asp:GridView> to a CSHTML file.

marcind is correct, if you want to open your existing ASP.NET website in WebMatrix and work on it from within the tool. If, on the other hand (or in addition to), you want to use Razor syntax in your site and stay within VisualStudio, check out this article: http://weblogs.asp.net/rashid/archive/2010/07/10/use-razor-as-asp-net-mvc-viewengine.aspx

There are four things you need to do:

  1. Add References to the Razor assemblies installed with WebMatrix. These can be found at C:Program FilesMicrosoft ASP.NETASP.NET Web Pagesv1.0Assemblies

  2. Create a custom ViewEngine class, a View class that inherits from IView (not that hard, check out the source in the article above)

  3. Add your new ViewEngine in Global.asax Application_Start()

    ViewEngines.Engines.Add(new RazorViewEngine(("cs"));
    
  4. Create your view pages with a .cshtml extension, instead of .aspx

There are a few steps here, but it s quick work, and the source from the article above will get you a long way there.





相关问题
How to update shared view adminheader.cshtml from a model

I ve added a drop down into my shared adminheader.cshtl view and it needs to be dynamically updated from data from a table. So when I use another view like routeplan.cshtml I load the view from ...

ASP.NET MVC Razor view engine

After reading Scott Guthrie s blog entry about the new Razor view engine for ASP.NET MVC and reading this question comparing the available view engines. Razor seems to address most of the problems ...

Client Id for Property (ASP.Net MVC)

I m trying to do a label for a TextBox in my View and I d like to know, how can I take a Id that will be render in client to generate scripts... for example: <label for="<%=x.Name.ClientId%>"...

Get Current Area Name in View or Controller

How do you get the current area name in the view or controller? Is there anything like ViewContext.RouteData.Values["controller"] for areas?

MVC which submit button has been pressed

I have two buttons on my MVC form: <input name="submit" type="submit" id="submit" value="Save" /> <input name="process" type="submit" id="process" value="Process" /> From my Controller ...

热门标签