我在web Api Controller中有以下代码:
[HttpPost]
[Route("ForgotPassword")]
[AllowAnonymous]
public async Task<IHttpActionResult> ForgotPassword(ForgotPasswordViewModel model)
{
if (ModelState.IsValid)
{
var user = await UserManager.FindByEmailAsync(model.Email);
if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id)))
{
// Don't reveal that the user does not exist or is not confirmed
return BadRequest("Either user does not exist or you have not confirmed your email.");
}
try
{
// Send an email with this link
string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
string callbackUrl = Url.Link("Default",
new { controller = "User/ManageAccount/reset-password", userId = user.Id, code = code }); //From UserController
await UserManager.SendEmailAsync(user.Id, "Reset Password", "Please reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>");
return Ok();
}
catch (Exception ex)
{
return InternalServerError();
}
}
return BadRequest();
}
这是UserController代码:
public class UserController : Controller
{
public ActionResult ManageAccount(string id)
{
if (!string.IsNullOrEmpty(id))
{
string page = "~/html/" + id + ".html";
return new FilePathResult(page, "text/html");
}
return new FilePathResult("~/html/login.html", "text/html");
}
}
看来路线没看到UserController !!
这是路线:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
这是Web Api的路线:
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
我不知道我做错了什么?
我想向您询问有关从WebAPI单击生成的链接的过程的更多信息。当您访问第二个网址时,浏览器显示的结果是什么?来自Asp WebServer或浏览器错误页面的错误页面?
我想您要提及的错误是由于您忽略了网址中的服务器端口,导致浏览器无法找到您的网站。请注意,在IIS Express上进行调试会为您提供一个随机的Web服务器端口,如果您希望修复此端口,因为http的默认值为80,请使用IIS进行部署。
为了更容易理解,请保持http://localhost:7524/
而不是更改为http://localhost/
。我认为它会正常工作!!