ASP. NET MVC Learning Controller Chapter

Source: Internet
Author: User
Tags httpcontext

I. Preface

After a long time, I finally can continue my ASP. NET MVC, before we fully described the knowledge of routing, we will start the Controller and the action of the explanation.

ASP. NET MVC Learning Routing space (1)

ASP. NET MVC Learning Routing space (2)

ASP. NET MVC Learning Routing Space (3)

Two. Text

Use of 1.IController

This interface may not be used by many people, but the controller class we used to implement this interface, and as long as the implementation of this interface can be used as a controller , of course, a lot of internal processing will be done by ourselves.

Here I use the IController implementation to invoke different methods based on the routing parameters:

 1 public class Homecontroller:icontroller 2 {3 public String Index () 4 {5 return "&lt ;h1>index

First we implement the execute method of the IController interface, and then we define several methods that we need to invoke on the outside, and then get the action arguments in the Execute method. and the corresponding method is obtained through the reflection technique , if it can get to the method then call the method and return, otherwise return 404.

The above is just a simple sample, and most of the time we don't need to implement the Icontroler interface.

2. Mature Controller

Before we can see that if we use the IController to implement the controller will be very troublesome, but in fact we are ultimately using the controller class,

It has implemented a number of functions so that we can focus on functionality rather than the underlying implementation. People who have been developing the ASP must know that many of the data are

With request.querystring,routedata to get.

However, it is easier to get in ASP. NET MVC , such as the following code:

1 public class Usercontroller:controller 2     {3  4 public         actionresult Index (string path) 5         {6             VIEWBA g.path1 = path; 7             return View (); 8         } 9 10}

The code above simply saves the incoming path property in the view package for the page output.

(First we will create a new view page, the code of the page is as follows)

1 

Below we access the page through this path: http://localhost:1201/User/Index?path=12

Then the final page looks like this:

We can see that the value of the path property exists in the URI so the background path is a few, so the output on the page is a few, the careful person will find that input the values shown in the inside will also follow the path in the URI .

Above we only change through the URI , the following is the form form to pass the value of path , we enter a value in input , and then click Submit, we can see the following results:

Here we can see what the value of our input box is, and the value of the last page output is the value of this input.

3. Action Results

No matter how powerful the controller is, the page that the user wants will not be generated, as well as useless. Here we are going to learn several action results:

A.view

This action result, a lot of people are not unfamiliar, because after the controller is created there will be a default Index action which is to return to view, but here you have to know if directly return to view Then the corresponding view file will follow the views/controller name/action name. cshtml to find,

Of course, if you don't find it, you'll also search other public pages, but we can also specify for ourselves:

First we create a new subindex.cshtml view under views/user/ and then modify the Index method code in Usercontroller :

1 public ActionResult Index () 2 {3       return View ("subindex"); 4}

Here we can see that we are directly passing the name of the view as an argument to view, but these are just the views that control the display, and we eventually need to pass the data in the controller to the view,

For the view to show, here we can introduce four ways,ViewBag,ViewData,tempdata,Model, below we use a section of code to demonstrate under:

1 public class Usercontroller:controller 2     {3  4 public         ActionResult Index () 5         {6             Viewbag.sho W = "From ViewBag"; 7             viewdata["show"] = "from ViewBag", 8             tempdata["show"] = "from TempData", 9             Object obj = "from Model"; 10
   return View (obj);         }12 13}

Here we assign values in various delivery modes, and here's the corresponding view:

1 

Then we recompile and the final result after refreshing the page is as follows:

Here is a little to introduce, is TempData, in fact, it is mainly used to pass data to other pages, so he has some features, that is, when the data is read from the data will be purged from it, unless read using the Peek method, Then the data will not be marked for deletion,

You can use the Keep method if you want to deliberately remove a deletion tag from a data. Related examples I'm not going to list them here.

B. redirection

Since it is the development site, since there will be the function of the jump page, below we will focus on the redirection functionality provided in ASP .

The first is the simplest redirect, which is to jump directly to the path we provide:

1 public class Usercontroller:controller2 {3 public         ActionResult Index () 4         {5             return Redirect ("/home/index "); 6         }7}

Here we can see the Redirect we directly into the Home controller amount Index action Route path, we open this page can be found to jump.

If you learn HTTP , you will know that there is a permanent redirect in the redirect, and if we need to use it, we can use the redirectpermanent method.

Through the above we see this need to enter the complete path, of course, ASP. NET MVC has provided us with other quick ways, such as the following way:

(We need to add a show action to the Usercontroller and add the corresponding view at the same time)

1 Public     class Usercontroller:controller 2     {3 public         ActionResult Index () 4         {5             return Redirecttoa Ction ("Show"); 6         } 7  8 public         ActionResult Show () 9         {Ten             return View ();         }12     }

Here we see the redirecttoaction in the show, in fact, this represents this redirect will jump to show action, if you need to redirect forever can use Redirecttoactionpermanent method.

But sometimes we need a more flexible way to redirect, then we can use the following method:

1 public         ActionResult Index () 2         {3             return Redirecttoroute (New {controller = "Home", action = "Index"}); 4< c20/>}

Here we are directly by passing the controller parameters that make up the request, the action parameters constitute the final route, and of course we can pass other additional parameters into it,

Use the redirecttoroutepermanent method if you need to redirect forever.

C. Other

Here we do not each go for example, here are the other action results:

JSON : Converting C # classes to JSON format

JavaScript : passing JavaScript code

Content : Passing character data in other formats

file: Passing a stream of files

httpnotfoound : 404 error

Because of the recent busy test, so there is no time to write essays, this essay is to use the work at home every day to write some of the last write out. Of course, the level is not as delicate as the first two articles.

Transferred from: http://www.cnblogs.com/yaozhenfa/p/asp_net_mvc_controller.html

ASP. NET MVC Learning Controller Chapter

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.