简体   繁体   中英

How do i pass multiple parameters to c# method from browser URL

I ad trying to pass parameters to call the following C# method

public ActionResult GenerateInvoicePDFByInvoiceNum(string id,string apikey)
{
    IInvoiceRepository rep = db.GetInvoiceRepository();
    //
    var api = Guid.Parse(apikey);
    Invoice invoice = rep.GetByExpression(i => i.InvoiceNo.Equals(id) && i.Visit.Branch.Practice.APIKey.ToString().Equals(apikey)).FirstOrDefault();
    if (invoice==null)
    {
        return HttpNotFound();
    }

    //return new Rotativa.ActionAsPdf("PrintInvoice", new { id = invoice.Id })
    //{
    //    //CustomSwitches = "--load-error-handling ignore "
    //    CustomSwitches = "--disable-javascript"
    //};

    return RedirectToAction("GenerateInvoicePDF", new { id = invoice.Hash });

}

From the browser I am trying to call it with a call like this which worked when I had only one parameter, but I don't know how to change those to pass the second parameter

http://foobar.n.co.za/Newlook/PrintInvoice/GenerateInvoicePDFByInvoiceNum/72341d

Thanks

On your url: http://foobar.n.co.za/Newlook/PrintInvoice/GenerateInvoicePDFByInvoiceNum/72341d

"/72341d" represents an optional parameter.

Try to pass a query string. To do that, you need to specify the parameter name and value you want to pass.

http://foobar.n.co.za/Newlook/PrintInvoice/GenerateInvoicePDFByInvoiceNum?id=72341d&apikey=YOUR_API_KEY

You can pass multiple parameters as "?param1=value1&param2=value2"

http://foobar.n.co.za/Newlook/PrintInvoice/GenerateInvoicePDFByInvoiceNum?id=1&1pikey=2

You have three choices to do this:

  1. Pass parameters using query string like /?id=value&apikey=value

  2. Add a custom route in "RouteConfig" (~/App_Start/RouteConfig.cs) like below:

     routes.MapRoute( name: "RouteName", url: "{controller}/{action}/{id}/{apikey}" );
  3. Use attribute routing. To do this first enable it by calling the "routes.MapMvcAttributeRoutes()" method in "RouteConfig" and then apply the [Route(urlPttern)] attribute to the related action and pass the url pattern to the method

after applying solution 2 or 3 you can pass parameters in url like:

"/foobar.n.co.za/Newlook/PrintInvoice/GenerateInvoicePDFByInvoiceNum/72341d/apikeyvalue"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM