Talking about how to pass multiple parameters in Asp.net Mvc Action, asp. netmvc

Source: Internet
Author: User

Talking about how to pass multiple parameters in Asp.net Mvc Action, asp. netmvc

Recently, you need to query the log file information, view a specific log information, and view other log information lists of the date of the log.

To complete this function, I plan to input two parameters in the URL, one to record the log time and the other to record the log's primary key ID. Therefore, I want to start with the Asp.net MVC route.

In the Global. asax file, the default route is as follows.

Routes. mapRoute ("Default", // route name "{controller}/{action}/{id}", // URL with parameters new {controller = "Logon ", action = "Logon", id = UrlParameter. optional} // default value );

In this route, only one parameter can be entered after the Action, and multiple parameters cannot be entered. Therefore, you need to add route information.

Add a route entry named "Default1" in the Global file. The Code is as follows:

// The first route transmits two parameters to the routes. mapRoute ("Default1", "{controller}/{action}/{Parma1}/{Parma2}", new {controller = "", action = ""}, new {});

You can input two parameters for the preceding routing.

Here, we create a TestController, add a Test. cshtml page, and write the code in TestController, as shown below:

public ActionResult Test(string date, string id){ViewData["date"] = date;ViewData["id"] = id;return View();}

Write the following code on the Test. cshtml page:

The log time to be queried is @ ViewData ["date"] <br/> the log ID to be queried is @ ViewData ["id"] <br/>

Run the compiler and enter "http: // localhost: 11507/Test/2013-12-18/5" in the browser. The page is displayed as follows:

The log time to be queried is
Log ID: 5

Now there is another problem. We need to input multiple parameters. What should we do? Of course, only the new route "Default2" is added ". The Code is as follows:

// The second route transmits multiple parameters, only the data routes after the second underline is obtained. mapRoute ("Default2", "{controller}/{action}/{* id}", new {controller = "", action = ""});

Run the compiler and enter "http: // localhost: 11507/Test/2013-12-18/5/xianrongbin" in the browser. The page is displayed as follows:

The log time to be queried is:
Log ID: 2013-12-18/5/xianrongbin

Here we can only get all the parameters following the Action. For example, the log time is "", the log ID is "5", and the LOG Operator is "xianrongbin ".

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.