【问题标题】:Value cannot be null. Parameter name: routeCollection + ApiController值不能为空。参数名称:routeCollection + ApiController
【发布时间】:2022-01-13 11:40:27
【问题描述】:

在另一个 ASP.NET MVC 5 控制器项目中,AccountController 继承自 Controller 类。在这个类中,当我尝试生成一个 URL 来重置用户密码时,我有以下实现。

// Send an email with this link
string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);

但是,我将我的逻辑移到了一个 Web API 项目,其中 AccountControllerApiController 类中插入。为了生成 URL 以重置用户密码,我有以下实现:

var _url = new System.Web.Mvc.UrlHelper();
var code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
var callbackUrl = _url.Action("ResetPassword", "Account", new { UserId = user.Id, code = code }, Request.RequestUri.Scheme);

有了这个后来的实现,AccountController : ApiController,我得到一个错误:

"ExceptionMessage": "值不能为空。
参数名称:routeCollection"

我错过了什么?

【问题讨论】:

    标签: asp.net-mvc asp.net-web-api asp.net-identity


    【解决方案1】:

    显然,您必须将参数传递给 UrlHelper 类,如下所示

    var _url = new System.Web.Mvc.UrlHelper(HttpContext.Current.Request.RequestContext);
    

    【讨论】:

      猜你喜欢
      • 2016-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-16
      • 2014-05-17
      • 2015-11-05
      相关资源
      最近更新 更多