How to add code in ASP.NET MVC in Visual Studio?

I want to use the code next to the files for my views in my ASP.NET MVC project. Is there any easy way in Visual Studio 2008 to add code next to a file in a view?

Note. I know that code, except for files, is not preferred in ASP.NET MVC, but I believe that I want to provide .aspx files to the designer and I don't want to confuse it with nonHTML code as little as possible. More compelling reasons for this can be found here .

+1
source share
2 answers

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

Then, in changing the markup, aspx is inherited from the 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.

+2
source

A better bet might be to force ViewPage to inherit a custom class that provides the material you want as protected members or extension methods or something else.

However, most worthy designers these days understand: "Don't mess with this section at the top, which is located in <% ...%>."

+1
source

All Articles