今天第一次做微信。网页授权成功后应该跳转到应有的界面,然后就一直提示:尝试创建“MobileHuaXunTJ.Controllers.HomeController”类型的控制器时出错。请确保控制器具有无参数公共构造函数。错误写的很清楚,但是我在控制器加上无参构造函数之后也不行,不太懂依赖注入方面的东西,麻烦高手帮忙看一下,公司就两个程序员也找不到人一起找问题,一个人弄了一天都还是有问题,谢谢了!下面是代码走动过程:
baseController里的过滤器
public class VaildateLogin : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
string url = filterContext.RequestContext.HttpContext.Request.Url.ToString();
if (System.Web.HttpContext.Current.Session["LoginUserInfo"] == null)
{
filterContext.Result = new RedirectToRouteResult("Default", new RouteValueDictionary(new { controller = "Account", action = "Login", ReturnUrl = url }));
}
else
{
base.OnActionExecuting(filterContext);
}
}
}
第二步:
public RedirectResult Login(string ReturnUrl = "/Home/Index")
{
string url = ReturnUrl;
if (LoginUserInfo == null)
url = OAuthApi.GetAuthorizeUrl(AppId, BOSS.Utility.ConfigHelper.getConfig("OAuth2CallBackUrl") + "?ReturnUrl=" + url, stateCode, OAuthScope.snsapi_userinfo);
Utils.WriteLogFile("跳转地址:"+url, "授权成功");
return Redirect(url);
}
最后就是回调:
public ActionResult CallBack(string code, string state, string ReturnUrl)
{
if (string.IsNullOrEmpty(code))
{
return Content("您拒绝了授权!");
}
if (state != stateCode)
{
return Content("验证失败!请从正规途径进入!");
}
OAuthUserInfo userInfo = null;
OAuthAccessTokenResult result = null;
try
{
string access_Token = Access_tokenCode.IsExistAccess_Token();
System.Web.HttpContext.Current.Session["access_token"] = access_Token;
result = OAuthApi.GetAccessToken(AppId, AppSecret, code);
if (access_Token == null || access_Token == "")
{
return Content("授权出错!");
}
userInfo = OAuthApi.GetUserInfo(result.access_token, result.openid);
if (userInfo != null)
{
Session["wxuserInfo"] = userInfo;
}
//平台登录
user_basic loginUserInfo = new UserBasicBLL().CheckUserLogin(3, userInfo.openid, "");
if (loginUserInfo != null)
{
Session["LoginUserInfo"] = loginUserInfo;
}
else
{
ReturnUrl = "/Account/Register";
}
return Redirect(ReturnUrl);
}
catch (ErrorJsonResultException ex)
{
Utils.WriteLogFile(ex.ToString(), "用户授权");
return Content("用户已授权,授权Token:" + result.access_token);
}
}
红色标处就有问题,地址栏参数无错
红色的使用RedirectToAction("Action name","Controller name");
建议将AccountController的代码贴出来
你 return RedirectToAction试下,另外把Account这个控制器代码贴出来
我比你惨,就我一个程序员😂