开发者

ASP.NET MVC "Convert to Web Application" option is missing in Visual Studio

开发者 https://www.devze.com 2022-12-19 22:23 出处:网络
I\'m working with Visual开发者_StackOverflow Studio 2008 SP1 and ASP.NET MVC v1. When right clicking on a view I do not get the option \"Convert to Web Application\" that I would need to generate code

I'm working with Visual开发者_StackOverflow Studio 2008 SP1 and ASP.NET MVC v1. When right clicking on a view I do not get the option "Convert to Web Application" that I would need to generate code behind .cs classes. I see that option for the actual project and folders, but not for views (aspx files). I've checked the ProjectTypeGuids to have the "right" (?) values:

{603c0e0b-db56-11dc-be95-000d561079b0};{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}

Any other suggestions as to what I could look for?

Thanks.

(I am aware of design implications of using code behind classes with MVC)

P.S. To do it manually all you have to do is:

  1. Add a file with the same name as your view and the .cs (or .vb) extension, for example Index.aspx.cs. Make sure you modify your class to inherit from System.Web.Mvc.ViewPage or some other class that inherits from that.
  2. Edit the aspx file and add to the @Page directive CodeBehind="Index.aspx.cs" and change Inherits to "MyNamespace.Views.Home.Index" (obviously you need to have the right code behind and namespace there).
  3. Right click on the aspx file and choose Convert to Web Application. This will create the design file and also modify your .cs class and mark it as "partial".


"Convert to web application" is a project/file-level command. You can't use it on a single ASPX file.

Also, there is no alternative automated way (that I know of :-)) to add code-behind files to an ASPX file. You have to do it manually, by adding the relevant files yourself and then adding them to the .csproj.


There's no need to use 'code-behind' with ASP.NET MVC.

If you use a 'code-behind', you're not following the convention of ASP.NET MVC.

The question is, why do you want a code-behind? Answering that will help us to determine what you really need.

If you really want to do this, you can do it by mixing Webforms and ASP.NET MVC together. There are lots of resources on this, but here's just one.


The MVC development model does not need code behind.

Read a good Blog Post on this Here


If you're trying to reuse some controls, maybe a good approach is to create and render them inside a helper method and than call that method from the view.

What I'm thinking about would be something like this:

public static string HelperMethod(param_list)
{
   var control = new ControlType();
   //set up control properties according to param_list

   //get the html as string - one way to do it would be like this
   StringWriter stringWriter = new StringWriter();
   HtmlTextWriter htmlWriter= new HtmlTextWriter(stringWriter);
   control.RenderControl(htmlWriter);
   string result= stringWriter.ToString();
}

And then call it from the view like this:

<%= HelperClass.HelperMethod(params) %>

I'm not sure if this approach will work, I don't know even if it makes sense. It's more of I hack than a proper solution. I haven't done anything like this before, it's just an idea, try to see if it helps you. You should also have in mind that ASP.NET controls usually use the ViewState for state management and that there is no such thing in ASP.NET MVC.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号