c# - 路由没有找到控制器
问题描述
我在 webApi 控制器中有以下代码:
[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();
}
这是用户控制器代码:
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");
}
}
The generated link is like this: " http://localhost:7524/User/ManageAccount/reset-password?userId=4&code=ibrDwvzMjDerBPjRYFcKnATi6GpgIEGC1ytT6c%2Flsk4BF9ykxZWx8McBvxxBf%2F82csUGaCxpvgqY2eWUvirAxGJqP4I%2B9YHrVRpWmJN2u74xUP%2B%2BqAkfRf6d5gTz9kXVdWJQga2R1dpTy7tQC3OUWQ%3D%3D "
When I click it become like this: https://localhost/User/ManageAccount/reset-password?userId=4&code=ibrDwvzMjDerBPjRYFcKnATi6GpgIEGC1ytT6c%2Flsk4BF9ykxZWx8McBvxxBf%2F82csUGaCxpvgqY2eWUvirAxGJqP4I%2B9YHrVRpWmJN2u74xUP%2B%2BqAkfRf6d5gTz9kXVdWJQga2R1dpTy7tQC3OUWQ%3D%3D
似乎该路线没有看到 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 }
);
}
这是WebApi的路由:
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 生成的链接的过程。当您访问第二个 url 时,浏览器显示的结果是什么?来自 Asp WebServer 的错误页面或浏览器错误页面?
我猜您要提到的错误是由于您忽略了 url 中的服务器端口,这导致浏览器无法找到您的站点。注意,在 IIS Express 上调试会给你一个随机的 webserver 端口,如果你想固定这个端口,因为 http 的默认值是 80,用 IIS 部署它。
为了更容易理解,请保留http://localhost:7524/
而不是更改为http://localhost/
. 我认为它会正常工作!
推荐阅读
- java - Java 组布局取消接受按钮
- time-series - 如何根据特殊事件和营销活动调整预测
- r - 在 R 中的函数内断开 DBI / RSQLite
- wordpress - wordpress 搜索栏可以只显示文件而不显示帖子吗?
- css - Bootstrap 4 网格 - 左侧 3 个堆叠列,右侧 1 个
- networking - 夜间备份 50GB 表期间的 AWS 网络流量阻塞
- angular - 网格列数据表中的新行角度
- java - JNA 中的内存分配
- php - 尝试使用 php 获取子域 URL
- r - 使用 Rvest 遍历 URL 的数据框时,我收到“无效描述”错误。这是怎么回事?