Marc L
Marc L

Reputation: 887

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

Upvotes: 0

Views: 931

Answers (4)

RezA
RezA

Reputation: 173

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"

Upvotes: 0

Hossein Sabziani
Hossein Sabziani

Reputation: 3495

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

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

Upvotes: 1

Bogner Roy
Bogner Roy

Reputation: 199

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

Upvotes: 1

Related Questions