简体   繁体   English

如何从浏览器 URL 向 c# 方法传递多个参数

[英]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

You can pass multiple parameters as "?param1=value1&param2=value2"您可以将多个参数传递为"?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使用查询字符串传递参数,如 /?id=value&apikey=value

  2. 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}" );
  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为此,首先通过调用“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.

相关问题 如何从ac#客户端将多个参数传递给WCF Restful服务? - How do I pass multiple parameters to a WCF Restful service from a c# client? C# 重构 - 如何传递可能有多个参数的泛型方法? - C# Refactoring - How to pass generic method with possibly multiple parameters? 在C#中,如何使用linq将列表类型参数传递给方法? - In C#, using linq how do I pass list type parameters to a method? 将方法附加到C#中的事件时如何传递参数 - how do I pass parameters when attaching a method to an event in c# 如何将两个相似的具体对象传递给具有在C#中实现泛型的接口参数的方法? - How do I pass two similar concrete objects to a method with interface parameters that implement generics in C#? 如何在C#中传递多个函数作为约束参数? - How do you pass multiple functions as constrainment parameters in C#? 如何将多个参数从C#传递到Crystal报表 - How to Pass multiple parameters from C# to crystal reports C# 如何将可选参数作为集合传递? - C# how do I pass optional parameters as a collection? 如何将两个参数传递给 c# 中的 GraphQLHttpClient? - How do I pass two parameters to GraphQLHttpClient in c#? 如何在 C# 中将可空修饰符作为输出参数传递 - How do I pass in nullable modifiers as out parameters in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM