views:

222

answers:

2

I want to use code beside files for my views in my ASP.NET MVC project. Is there any simple way in Visual Studion 2008 how to add a code beside file to the view?

Note: I know that code besides files are no preferred in ASP.NET MVC but my reason is that I want to give .aspx files to designer and don't want to confuse him nonHTML code as little as possible. More good reasons for doing that can be found here.

+3  A: 

Add a class to your view folder and name it (for example) Foo and make sure that this class inherits ViewPage or ViewPage in case if your View is a strongly typed one.

Then in the aspx markup change the inherits from attribute of the @page directive to Foo.cs

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="Foo" CodeBehind="~/Views/Home/Foo.cs" %>

Hope this helps.

Galilyou
Well this is fine. However it's code behind no code beside. I want to compile .aspx and .cs file into one class.
Jakub Šturc
+1  A: 

A better bet might be to make your ViewPage inherit a custom class which exposes the stuff you want as protected members or extension methods or whatever.

That said, most decent designers these days understand "don't mess with that section at the top that is in the <% ... %>."

Wyatt Barnett