//微信公众号
private static string OfficialAccountAppId => ConfigurationManager.AppSettings["OfficialAccountAppId"];
private static string OfficialAccountAppSecret => ConfigurationManager.AppSettings["OfficialAccountAppSecret"];
/// <summary>
/// 返回带参数(场景值)的公众号二维码图片地址
/// </summary>
/// <returns></returns>
[Route("weixin/createqrcode/withParam")]
[HttpGet]
public async Task<RestResponse> GetOfficialAccountQRcodeWithParam(string scene_str)
var ticket = CreateQrCodeTicketAsync(scene_str);
RestResponse result = new RestResponse();
//通过ticket获取二维码对应的url
result.Content = QrCodeApi.GetShowQrCodeUrl(ticket); //利用盛派获取图片地址(本人对网络请求并不熟悉,这里只是知道调用该方法能够获取成功,这里并没有去细究)
return result;
/// <summary>
/// 利用微信API创建二维码ticket
/// </summary>
/// <param name="scene_str">场景值</param>
/// <returns></returns>
private string CreateQrCodeTicketAsync(string scene_str)
var accessToken = WeiXinCommonBll.WxGetAccexxToken(OfficialAccountAppId, OfficialAccountAppSecret); //微信获取access_token的方法每天有请求次数限制,应该自己封装一下
string wxurl = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" + accessToken;
string strJson = "{\"expire_seconds\": 86400, \"action_name\": \"QR_STR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\": " + scene_str + "}}}";
var result = Post(wxurl, strJson);
if (result != null)
JObject jobect = JObject.Parse(result);
string ticket = (string)jobect["ticket"];
if (string.IsNullOrEmpty(ticket))
return null;
return ticket;
return null;
private string Post(string wxurl, string strJson)
WebClient client = new WebClient();
client.Credentials = CredentialCache.DefaultCredentials;
string result = client.UploadString(wxurl, "POST", strJson);
return result;
因为盛派封装的方法上说只有当 action_name 为 QR_LIMIT_STR_SCENE(永久的字符串参数值)时,后面的参数值 scene_str 才会有效,所以才在上面又自己写了一下。
但是,经测试,其实临时的字符串也是可以用的。所以,更简单的方式是用盛派封装的方法来实现:
//微信公众号
private static string OfficialAccountAppId => ConfigurationManager.AppSettings["OfficialAccountAppId"];
private static string OfficialAccountAppSecret => ConfigurationManager.AppSettings["OfficialAccountAppSecret"];
/// <summary>
/// 返回带参数(场景值)的公众号二维码图片地址
/// </summary>
/// <returns></returns>
[Route("weixin/createqrcode/withParam")]
[HttpGet]
public async Task<RestResponse> GetOfficialAccountQRcodeWithParam(string scene_str)
RestResponse result = new RestResponse();
var accessToken = WeiXinCommonBll.WxGetAccexxToken(OfficialAccountAppId, OfficialAccountAppSecret);
//原以为真的只有为永久字符串的时候 scene_str 才会有效,但是经测试,其实临时字符串的时候也是可以用的!
var qrTicket = await QrCodeApi.CreateAsync(accessToken, 500, 5, QrCode_ActionName.QR_STR_SCENE, scene_str);
result.Content = QrCodeApi.GetShowQrCodeUrl(qrTicket.ticket);
return result;