[英]How do i pass multiple parameters to c# method from browser URL
I ad trying to pass parameters to call the following C# method我尝试传递参数来调用以下 C# 方法
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在您的 url 上: http://foobar.n.co.za/Newlook/PrintInvoice/GenerateInvoicePDFByInvoiceNum/72341d
"/72341d" represents an optional parameter. “/72341d”代表可选参数。
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 http://foobar.n.co.za/Newlook/PrintInvoice/GenerateInvoicePDFByInvoiceNum?id=72341d&apikey=YOUR_API_KEY
This is how you pass parameters by url:这是 url 传递参数的方式:
http://foobar.n.co.za/Newlook/PrintInvoice/GenerateInvoicePDFByInvoiceNum?id=72341d&apikey=yourapikeygoeshere http://foobar.n.co.za/Newlook/PrintInvoice/GenerateInvoicePDFByInvoiceNum?id=72341d&apikey=yourapikeygoeshere
You can pass multiple parameters as "?param1=value1¶m2=value2"
您可以将多个参数传递为"?param1=value1¶m2=value2"
http://foobar.n.co.za/Newlook/PrintInvoice/GenerateInvoicePDFByInvoiceNum?id=1&1pikey=2
You have three choices to do this:您有以下三种选择:
Pass parameters using query string like /?id=value&apikey=value使用查询字符串传递参数,如 /?id=value&apikey=value
Add a custom route in "RouteConfig" (~/App_Start/RouteConfig.cs) like below:在“RouteConfig”(~/App_Start/RouteConfig.cs)中添加自定义路由,如下所示:
routes.MapRoute( name: "RouteName", url: "{controller}/{action}/{id}/{apikey}" );
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为此,首先通过调用“RouteConfig”中的“routes.MapMvcAttributeRoutes()”方法启用它,然后将 [Route(urlPttern)] 属性应用于相关操作并将 url 模式传递给该方法
after applying solution 2 or 3 you can pass parameters in url like:应用解决方案 2 或 3 后,您可以在 url 中传递参数,例如:
"/foobar.n.co.za/Newlook/PrintInvoice/GenerateInvoicePDFByInvoiceNum/72341d/apikeyvalue" “/foobar.n.co.za/Newlook/PrintInvoice/GenerateInvoicePDFByInvoiceNum/72341d/apikeyvalue”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.