⬆️ ⬇️

On the experience of translating the asp.net mvc project to .net 4 (mvc 2)

Hello! In this article I will tell you about my experience in converting an asp.net mvc project to .net 4. There is a post on the net called ASP.NET MVC 2 Brings Breaking Changes . Here you can familiarize yourself with the main innovations in asp.net mvc 2. My project migration experience concerns both this post and the fact that it is not mentioned. I will say right away that after the conversion, the project was compiled, but was inoperable. Did not respond to actions, or generated exceptions. Let us analyze each feature I found in order.



Changed JsonResult behavior



Let's start with a class like JsonResult. I work with him jQuery Grid. In MVC 2, JsonResult by default began to invert JSON not valid for GET requests, i.e. My grid received not valid JSON and could not reject it. It is treated by changing the call.



//convert to JSON and return to client

// GET MVC1

return Json(result);



//convert to JSON and return to client

// MVC2,

return Json(result,JsonRequestBehavior.AllowGet);




HTML helpers now generate only valid form element IDs.



In my project, the library is used for client-side and server-side validation based on validators and attributes from MS EnterpriseLibrary. The code for validation on the client is generated by a special class. The selection of form elements for validation on the client (script) is based on their ID. MVC1 generated an ID based on the name of the object property being rendered, that is:

<%= Html.TextArea("_", Model._, 2, 30)%>

Generated by:

textarea cols="30" id="_" name="_" rows="2"

In MVC2, this behavior has changed: the helper generates only valid IDs, that is, in our case, not containing Cyrillic characters. And ... client validation did not work, because The ID of the item RealName was not generated, and the script tried to find it.

Solving the problem for MVC2:

<%= Html.TextArea("_", Model._, 2, 30, new { id = "_" })%>



This is not a good decision, because I'm using hard coding. But thanks to the MVC pattern, you almost always know where you need it and what to change in case of changes in names. In MVC2, typed helpers appeared that help get rid of this bad work with strings in views.

')

Binder to the empty fields of the html form instead of String.Empty assigns null



The essence is reflected in the title. Suppose I have a nullable property in the class, but there is a validator on it:

[StringLengthValidator(250, MessageTemplate = " 250 ")]

public string {get; set;}




In MVC1, binder on a blank field sets String.Empty and the validator executes without errors. String.Empty - a string of zero characters and it skips it.



In MVC2, binder sets null, and the validator raises an exception. Validation fails.



To solve the problem, I did not write my binder, because this adds extra complexity to the project. I think that the project should have as few samopisnyh elements as possible, and for the sake of such a minor correction, it is impractical to write your binder.

I changed the properties of the object as follows:

(This piece of functionality is well localized, we can say that it does not introduce additional dependencies)



[StringLengthValidator(250, MessageTemplate = " 250 ")]

public string

{

get

{

return this._.;

}

set

{

if (value == null) this._. = string.Empty;

else

this._. = value;

}

}




Binder reads fields marked with bind = exclude



We have the following class:

[System.Web.Mvc.Bind(Exclude = "_,_")]

public class View : IView

{

public View()

{

this._ = new ();

this._.Reference.EntityKey =

new System.Data.EntityKey("ModelWebUchetCon.", "_", 0);

this._.Reference.EntityKey =

new System.Data.EntityKey("ModelWebUchetCon.", "_", 0);

this._.Reference.EntityKey =

new System.Data.EntityKey("ModelWebUchetCon.", "_", 0);

this._.Reference.EntityKey =

new System.Data.EntityKey("ModelWebUchetCon.", "_", 0);

this._.Reference.EntityKey =

new System.Data.EntityKey("ModelWebUchetCon.", "_", 0);

this._. = string.Empty;



}

public View( Base)

{

this._ = Base;

}



}


We have two constructors - the first to create a completely new object, and the second to create an object based on an existing one. The first constructor initializes its fields with values, which will then be rewritten. In MVC1, binder works (for the purpose of validation, reading, writing) only with those fields that are not filtered by the Bind attribute, so I do not initialize them in the first constructor. In MVC2, the validation of the entire model is used, that is, all object properties are checked for validity, and therefore all are read, including properties not initialized by the first constructor, and NullReference is raised. To overcome this behavior, we had to turn on the logic for initializing these properties. Maybe someone will write an attribute for the class, which disables the full validation of the model. There is an example of such an attribute for the entire controller.



Changes in the web.config file



In .NET 4.0, web.config has visibly cleaned up. Previously, all add-ins that appeared after .NET 2.0 were registered in this file. Now that the new CLR has been released, this need has disappeared and you can noticeably clean your configs. To ensure that everything goes smoothly I recommend doing this:



Conclusion


As we know the transition to the new version of .net is not always painless. Microsoft every two years pleases and disappoints us. In this article, I described basically what made me somewhat disappointed and sweat. It was necessary to re-test the project, search for and fix non-obvious run-time errors (everything worked before). True, I managed to find errors related to the fact that IE caches ajax requests. That is, I found a real mistake that took place in the project. There is a plus.

Source: https://habr.com/ru/post/96825/



All Articles