首页 > 解决方案 > 路由没有找到控制器

问题描述

我在 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 }
        );


    }

我不知道我做错了什么?

标签: c#htmlasp.netasp.net-mvcasp.net-web-api

解决方案


我想问你更多关于点击从 WebAPI 生成的链接的过程。当您访问第二个 url 时,浏览器显示的结果是什么?来自 Asp WebServer 的错误页面或浏览器错误页面?

我猜您要提到的错误是由于您忽略了 url 中的服务器端口,这导致浏览器无法找到您的站点。注意,在 IIS Express 上调试会给你一个随机的 webserver 端口,如果你想固定这个端口,因为 http 的默认值是 80,用 IIS 部署它。

为了更容易理解,请保留http://localhost:7524/而不是更改为http://localhost/. 我认为它会正常工作!


推荐阅读