asp.net mvc action How to pass multiple parameters __.net

Source: Internet
Author: User

Recently, there is a need to work: Users query log file information, view a specific log information, may also view the log date of the other log information list.

To complete this function, I intend to pass two parameters in the URL, one to record this log time, and the other to log the primary key ID, so prepare to route from ASP.net mvc
Start

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

Routes. Maproute (
                "Default",///Routing name
                "{controller}/{action}/{id}",//URL
                new {controller with parameters) = "Logon", action = " Logon ", id = urlparameter.optional}//parameter default value
            );

In this route, you can only pass in one parameter after the action, and you cannot pass in more than one parameter, so you need to increase the routing information.

In the global file, a new route is added, the route name is "DEFAULT1", and the code is as follows

The first route passes two parameters past
routes. Maproute ("Default1",
"{controller}/{action}/{parma1}/{parma2}",
new {controller = "", Action = ""},
new { });

The above route, you can pass in two parameters.

Here, we build a testcontroller, add a test.cshtml page, write the code in TestController, as follows

Public ActionResult Test (string date, string id)
{
viewdata["date" = date;
viewdata["id" = ID;
return View ();
}

Write the following code in the Test.cshtml page

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

Run the compiler, and then enter "HTTP://LOCALHOST:11507/TEST/TEST/2013-12-18/5" in the browser, the page appears as follows

The log time to query is: 2013-12-18
The log ID to query is: 5

Now there is a problem, you need to pass in multiple parameters, how to do it. Of course, only the new route "DEFAULT2" is added. The code is as follows

The second route passes multiple parameters, but only after the second underline is obtained the data
routes. Maproute ("Default2",
"{controller}/{action}/{*id}",
new {controller = "", Action = ""}

);

Run the compiler, and then enter "Http://localhost:11507/Test/Test/2013-12-18/5/xianrongbin" in the browser, the page appears as follows

The log time to query is:
The log ID to query is: 2013-12-18/5/xianrongbin

Here we can only get all the parameters behind the action, for this, we could parse out, such as the log time is "2013-12-18", the Log ID is "5", the log operator is "Xianrongbin".

Ok, the whole thing is complete ~ ~ ~

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.