ASP. net mvc 1.0 RC version released

Source: Internet
Author: User
Tags http cookie

[Original address] ASP. net mvc 1.0 Release Candidate now available
[Original article posting date] Tuesday, January 27,200

Today, we have released a candidate version (RC) for ASP. net mvc 1.0 Final version ). Click here to download (Note: This link is only available. If the link does not work, please wait for a few minutes for the server to be accessed to be updated ). This version can be used in Visual Studio 2008 and free Visual Web Developer 2008.

Today's ASP. net mvc rc version is the last public version before the final release of version 1.0. We expect the final version of ASP. net mvc 1.0 to be released next month.

In addition to bug fixes, today's version also contains several new features. It also includes several improvements to existing functions based on customer feedback. Read the release notes released with ASP. net mvc download to learn the complete details of all changes. This release instruction also contains instructions on how to upgrade existing applications built with ASP. net mvc Beta to RC.

Improvements in Visual Studio Tools

The RC version includes several new features in Visual Studio (except the new features provided in Beta, which I will not discuss here). These features include:

Add controller command

Now you can go to ASP. in the. net mvc project, type ctrl-M and Ctrl-C, or right-click the/controller folder and select "Add-> controller (add Controller) "context menu item to create a new controller class:

This will bring up the "add controller" dialog box, allowing you to name the controller you want to create and whether you want to automatically generate common crud methods (scaffold:

Click "add" to generate a controller class and add it to the project:

Add view command

You can enter Ctrl-M, Ctrl-V in the Action Method of a controller, or right-click an action method, select the "add view" context menu to generate a new view template:

This will bring up the "add view" dialog box, allowing you to name the new view you want to create (with predefined options in advance ). It allows you to create "empty (blank)" view templates, or automatically generate or scaffold view templates based on the object type passed to the view by the Controller action method. The scaffolding infrastructure uses reflection when generating a view template, so that it can generate a new template based on any poco (plain old CLR object, a common CLR object) that is passed to it. It is independent of any special ORM or data implementation.

For example, below, we want to generate a "list" view template based on the product Object Sequence passed in from the preceding action method:

Click "add" to generate a view template in the \ views \ products \ folder, which contains the default "scaffolding" implementation:

Then, we run the application and request it in the browser./ProductsURL to view the product list:

RC releases several built-in scaffolding templates: "empty (blank)", "list", "details (details)", and "edit) and create (create) (you can also add your own scaffolding template, More details will be provided later ).

For example, to support product editing, we can implement the HTTP-GET version of the "edit" Action Method on the products controller as follows, and then call the "add view" command:

In the "add view" dialog box, we can indicate that we will pass a product object to our view, and then select the "edit" template option to generate the scaffolding implementation:

Click the Add button to generate an edit view template in the \ views \ products \ folder, which contains the default scaffolding implementation:

Then, we can run the application and request it in the browser./Products/edit/1URL to edit the product details:

To save the edited changes, we can implement the "edit" Action Method HTTP-POST version in the products controller:

Note thatCodeIn the case of an error (for example, someone input an invalid string to a numeric value), how can we re-display the view. The "edit" and "CREATE" scaffolding templates contain, auxiliary HTML verification methods required to retain user input and invalid input elements with red-letter signs:

In the end, you probably won't use the templates generated by scaffolding as they are, and these templates will often be replaced completely. However, you can get an initial implementation and quickly run it. It is very useful to have a starting view template that you can easily fine tune for your scenario.

Because the scaffolding infrastructure supports generating a scaffold view for any common CLR object, you can use it for domain model objects (including using LINQ to SQL, LINQ to entities, nhibties, llblgen pro, subsonic, and other popular ORM implementation ing objects), can also be used for custom presentation model/viewmodel classes.

 

Add and customize scaffolding templates

The scaffolding infrastructure of ASP. net mvc is implemented using the built-in T4 template architecture of Visual Studio (Scott hanselman has a good blog on T4.

You can customize/modify any built-in ASP. net mvc scaffolding template implementation. You can also create other scaffolding templates (for example, scottgu crazy look scaffold options) to display them in the "add view" dialog box as an option.

To customize/Add a scaffold template at the entire machine level, open"C: \ Program Files \ Microsoft Visual Studio 9.0 \ common7 \ ide \ itemtemplates \ CSHARP \ WEB \ MVC \ codetemplates"Folder:

The addcontroller sub-folder contains the scaffold template in the "add controller" dialog box, while the "addview" sub-folder contains the scaffold template in the "add view" dialog box:

The scaffold template filled in the "add view" dialog box only contains ". TT "File Extension text files, these". the TT text file contains the C # Or VB code in the row that will be run when the template is selected:

You can open and edit any existing files to customize the default scaffolding behavior. You can also add a new ". TT template file-like the Scott crazy look I added above. TT files are the same. After a new template file is added, the "add view" dialog box is updated, which is automatically included in the list of available scaffolding options:

In addition to customizing or adding template files at the machine level, you can also add or modify these template files at the individual project level, which also allows you to check these templates into the source code control system, they are easily used across the entire team.

You can add a codetemplates folder under the project to customize the scaffold template at the project level. Then you can create the "addcontroller" and "addview" subfolders in the folder:

You only need to add a ". tt" file with the same name in the project to overwrite any template file of the default machine level. For example, we overwrite the default "controller. TT" scaffold template used in the "Add controller" scenario.

You can place the new view scaffold template file in the "addview" folder and add it to the list. For example, we added a "yet another crazy look. TT" view template in the project. When you use the "add view" dialog box, we will now see a set of all templates defined at the machine and project levels:

Note: When you set when the TT "template is added to the \ codetemplates folder, make sure that each". the "custom tool (custom tool)" attribute of the TT template file is set to a blank string (otherwise, you will get an error at runtime ), you may also need to close and re-open the project to clear a pseudo error in the error list. Soon, we will post more blog posts about creating/customizing scaffolding templates.

Go to Controller/go to view command

RC now supports quick navigation between controllers and views in the project.

When you move your mouse over the action method of the controller, you can type ctrl-M and Ctrl-g to quickly navigate to the corresponding view template. You can also right-click the current action method and select the go to view menu option to achieve the same navigation jump:

In the above example, the "go to view" command is used in the "edit" Action method of the productscontroller class. This will open \ views \ products \ edit in. aspx view template with default focus:

In the View template, you can also type ctrl-M and Ctrl-g to quickly navigate to the Controller class corresponding to the view. You can also right-click the view template and select the go to Controller menu option to implement the same navigation operation:

Compile the msbuild task of the View

By default, When you compile an ASP. net mvc project, it compiles all the Code except the code in the view template file in the project. In ASP. net mvc beta, If You Want To compile the code in the view template, You need to compile your own msbuild task. The ASP. net mvc rc version contains a built-in msbuild task. You can use it to include the view as part of the project compilation process. This task will verify the syntax of all views, parent pages, some views, and in-line code in the application. If there is a problem, it will give you a compilation error.

However, due to performance, we do not recommend that you run it during development for rapid compilation, but add it to specific compilation configurations (such as staging and deployment) or it is very convenient to use it with build or Ci (continuous integration) servers. See the detailed steps for enabling this task in the release notes.

Support for refactoring views

When you rename the Controller class or action method using the "RENAME (rename)" refactoring command of vs 2008, the file and folder names in the \ views subfolders are automatically updated. When the Controller class is updated, vs 2008 applies the standard convention-based naming mode to the existing view files/folders.

View Improvement

The RC version includes several view-specific improvements based on the feedback from the preview version.

View without background code file

Based on feedback, we changed the default view template to no longer include background code files. This change helps to enhance the view's purpose in MVC applications (the view is purely displayed, should not contain any code unrelated to the display), removed unused files in the project (for most people ).

The RC version now supports C # and VB syntaxes that inherit view templates from basic classes that use generics. For example, we use this syntax in the edit. aspx view template below. Its "inherits" attribute is inherited from the viewpage <product> type:

One advantage of not using the background code file is that after adding the view template file to the project, you can now get intelliisense immediately. In previous versions, you need to compile the view once after it is generated to obtain intelliisense. The RC version makes the process of adding and editing view files immediately no longer need to be compiled, and the process is smoother.

Important: if you are using the ASP.. Net MVC project upgraded to the RC version. Follow the steps in the release instructions, that is, the web. some settings in the config file need to be updated. The above generic-based syntax will work.

Model attributes

In the previous ASP. net mvc version, you used the viewdata. model attribute to access the forced model object passed to the View:

The above syntax still works, although you can still use the "model" attribute on the top of the viewpage:

The function of this attribute is exactly the same as the preceding code example. Its main advantage is that it allows you to write concise code. It also allows you to avoid using the viewdata dictionary when you want a view template to only deal with the strongly typed model that is passed to it.

Set title

Add to the new ASP. the default homepage template of the net MVC project has an <asp: contentplaceholder/> element in the This feature also introduces a defect. For details, see Phil haack's blog controls collection cannot be modified issue with ASP. net MVC RC1. You need to download an updated version of RC. For more information, see Phil haack's blog ASP. net mvc rc refresh)

For example, to customize the <title> Of the edit view to include the name of the current product, we can add the following code to edit. in the aspx template, the title is directly generated based on the model object uploaded to the View:

Then, the code above will make the Browser display the title with the product name during running:

In addition to setting the <title> element, you can also use the above method to dynamically add other

Strong html/ajax helper classes

One of the requirements raised by many people is the ability to use the syntax (rather than the string) of a strong expression when referring to a model when using HTML and Ajax auxiliary objects of a view.

In ASP. in the net MVC beta version, this is impossible, because the htmlhelper and ajaxhelper helper classes do not show the model type in their signatures, therefore, you need to build an auxiliary method that is directly based on the viewpage <tModel> base class to achieve this goal.

The ASP. net mvc rc version introduces new htmlhelper <tModel> and ajaxhelper <tModel> types, which are displayed on the base class of viewpage <tModel>. These types now allow anyone to construct a model that uses strong HTML and Ajax-assisted extensions that use expression syntaxes to reference a view. For example:

Core ASP. net mvc V1ProgramThe HTML form-assisted Extension Method in the set will also use string syntaxes that are not expression-based. The MVC futures Assembly released today (which can work in the RC Version) there are several initial implementations of form-assisted methods based on expression syntax. We plan to iterate these methods several times and add them to ASP in the next version. net MVC core program set.

Of course, you can add your own helper methods (using string-based or strong expression methods ). Built-in html/ajax auxiliary methods can be removed (because they are extension methods), if you want to replace or overwrite them with your own version.

Improvements in Form submission

The RC version includes several improvements specific to form submission:

[BIND (prefix = "")] is not required in common scenarios.

The RC version does not require you to explicitly use the [bind] feature (or set its prefix value "") in order to map the incoming form submission parameters without a prefix to complex action method parameters.

To understand its meaning, let's implement the "Create" scenario of productscontroller. Let's first implement the HTTP-GET version of the "Create" Action method, and we use the following code to return a view based on a blank product object:

Then, right-click the action method and select the "add view" command to generate a framework of the "Create" view template based on a product object:

Note that the preceding html. Textbox () method references the "productname" and "supplierid" attributes on the product object. This generates an HTML identifier like the following. The input "name" attribute is "productname" and "supplierid ":

Then we can implement the HTTP-POST version of the "Create" Action method, which accepts a product object as a parameter:

In ASP. in the net MVC beta version, we need to add a [BIND (prefix = "")] feature before the above product parameter. Otherwise, Asp. net MVC binding infrastructure will only look. "prefix form submission value (for example, producttocreate. productname and producttocreate. supplierid), instead of finding the submitted values of our form (because these values do not have the corresponding prefix ).

In the RC version, the default action method binder also tries to map the producttocreate. productname form value to the product object. However, if they cannot find such values, they will now try to map "productname" to the product object. This makes the syntax of scenarios where you need to pass complex objects to the Action Method clean and concise. You can use this function when you map a domain object (like the product object above) and the presentation model/viewmodel class (like the productviewmodel class.

A complete implementation of our create action method (including basic input type error handling) will look like this:

Now, our create action method will save the product object, if all values are entered correctly. When a user tries to create a product by using an invalid product property value, for example, replacing a valid decimal value with a "bogus" string, the form will be re-displayed, mark invalid input elements with red letters:

Improvement on modelbinder API

The model binding infrastructure in ASP. net mvc rc has been reconstructed, and additional extensions are added to allow custom binding and validation definitions. You can read the release notes of ASP. net mvc rc to learn about these details.

In addition to registering as a class, the current model binder can also be registered as an interface.

Supported idataerrorinfo

The default model binder in ASP. net mvc now supports classes that implement the idataerrorinfo interface. This allows you to throw verification error messages in a common way, which can be shared in Windows Forms, WPF, and current ASP. net mvc applications.

Improvements in Unit Testing

ASP. net mvc rc includes some significant improvements in unit testing:

Controllercontext is no longer inherited from requestcontext

The RC version includes refactoring the controllercontext class, which greatly simplifies common unit test scenarios. Controllercontext no longer inherits from requestcontext, but encapsulates requestcontext and presents it as an attribute. The attributes and inheritance classes of controllercontext are now virtual, rather than sealed, which greatly facilitates the creation of mock objects.

To understand its help, let's consider an action method like the following, which uses the built-in "request" and "user" objects:

In the previous ASP. net mvc version, testing the above action method requires simulating requestcontext and controllercontext (some non-obvious constructors are introduced in the routedata object ).

In RC, we can now perform unit tests like the following (using the Moq framework to simulate the controllercontext of our controller, allowing us to simulate the request. isauthenticated and user. Identity. Name attributes ):

This refactoring improvement not only helps to test the Controller's action method, but also helps test the filters, paths, and custom actionresult types, which are already used in other scenarios.

Unit Test of accountscontroller

Included in the RC version of ASP. the net MVC Project template now adds 25 pre-fabricated unit tests to verify the behavior of the accountscontroller class (this class is added to the project by default, used to handle logon and account management scenarios ). This facilitates restructuring and changing the accountscontroller. The implementation of accountscontroller has also been modified to facilitate the integration of permission systems based on non-member provisioners.

Cross Site Request Forgery (csrf)

Cross-Site Request Forgery (xsrf) attacks may cause users using trusted browser proxies to take unplanned actions on a website. These attacks depend on the fact that a user may have logged on to another website. A malicious website can create a request to the starting website (for example, use on the hacker's website to link to the URL of the starting website. This request is made in the user's browser and uses the user's authentication creden. Attackers want the user's authentication or session cookie to be valid. In this case, attackers can sometimes take destructive actions. Here you can learn the details of the hacker technology.

ASP. net mvc rc now includes some built-in auxiliary methods to prevent csrf attacks, which can help mitigate csrf attacks. For example, you can use the HTML. antiforgerytoken () helper method to generate a hidden input mark in the form:

This helper method generates an HTTP cookie and generates a hidden input element in our form. Malicious websites cannot obtain these two values at the same time.

Then we can apply a new [validateantiforgerytoken] feature on any action method we want to protect:

This checks for the presence of the appropriate mark and, if not, blocks the running of our HTTP-POST action method (reducing the chance of successful csrf attacks ).

File Processing improvements

ASP. net mvc rc contains several file processing improvements:

Auxiliary fileresult and file () Methods

RC adds a new fileresult class, which can be used to indicate that a file will be returned as an actionresult of a controller action method. The controller base class also contains a set of file () methods to facilitate the creation and return of fileresult.

For example, if we want to build a photo management website, we can define a simple photo class as follows, which encapsulates the details of the photos to be saved:

Then, we can use the new file () auxiliary method as follows to implement a displayphoto Action Method in the photomanager controller to Display photos in the database. In the following code, we pass the bytes to be displayed and the MIME type of the file to the file () auxiliary method, if we direct the element to our action method, the browser will display the corresponding photo on the page:

If we want end users to download photos and save them locally, we can implement a downloadphoto action method as follows. In the following code, we pass in the third parameter. This parameter will cause ASP. net mvc to set a header so that the browser displays a "Save .." In the dialog box, prefill the file name we provide in advance:

When you click the link pointing to/photomanager/dowloadphoto/1232 URL, you will be prompted to save the photo:

File Upload support

 

The RC version also includes built-in model binder support for uploading files and multi-part mime content.

For example, we can have a <form> with its enctype attribute set to "multipart/form-Data", and submit a form to/photomanager/uploadphoto URL. If there is a <input type = "file" name = "filetoupload"/> element in the form, you can select a file from the terminal. This file will be passed as an httppostedfilebase object to our action method:

Then we can use the httppostedfilebase object to access the original bytes of the uploaded file, its MIME type, and save it to the database or hard disk.

Ajax improvements

The ASP. net mvc rc version includes several Ajax improvements:

The jquery intelliisense file is included in the ASP. net mvc Project template.

The newly created ASP. the net MVC project now includes standard jquery library files (both complete and compressed versions) and vsdoc intelliisense document files, this file is used by Visual Studio to provide more intelliisense support (you can learn more here ):

This can facilitate the rich jquery JavaScript intelliisense in the client code blocks and JavaScript files:

Jquery 1.2.6 is released in the RC version today. We plan. net MVC 1.0 Final Version releases the forthcoming release of jquery 1.3.1, which will also include an updated version of the corresponding JavaScript intelliisense file.

Request. isajaxrequest Property

The request. isajaxrequest attribute can be used to check whether a request comes from a client's Ajax call (this is useful when the Ajax function is not enabled and you need to gracefully downgrade it ). In the RC version, the logic in this method has been changed to recognize the HTTP header "X-requested-with" (except.. net ). This is a well-known header information, which is sent by many JavaScript libraries (such as prototype, jquery, and dojo). This facilitates a unified approach in ASP.. Net MVC request to check whether Ajax is called.

Helper criptresult actionresult and JavaScript ()

The controller base class now contains a javascript () helper method that returns a new actionresult, whose type is javascriptresult. This function supports returning the original Javascript script from the server, and then running the built-in ASP. net mvc auxiliary method on the client. This is useful when you want to execute conditional JavaScript on the client based on the server logic.

Conclusion

We are very excited that we are in the final sprint phase of ASP. net mvc V1. Please report any problems you have found in the RC version as soon as possible so that we can solve these problems in the final version. The Development Team plans to carefully listen to any feedback in the next few weeks. Assuming there are no major problems, the official V1 version will be released next month.

I hope this article will help you,

Scott

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.