路线没找到控制器

问题描述 投票:0回答:1

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

生成的链接是这样的:“http://localhost:7524/User/ManageAccount/reset-password?userId=4&code=ibrDwvzMjDerBPjRYFcKnATi6GpgIEGC1ytT6c%2Flsk4BF9ykxZWx8McBvxxBf%2F82csUGaCxpvgqY2eWUvirAxGJqP4I%2B9YHrVRpWmJN2u74xUP%2B%2BqAkfRf6d5gTz9kXVdWJQga2R1dpTy7tQC3OUWQ%3D%3D

当我点击它变成这样: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 }
        );
    }

这是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 }
        );


    }

我不知道我做错了什么?

c# html asp.net asp.net-mvc asp.net-web-api
1个回答
0
投票

我想向您询问有关从WebAPI单击生成的链接的过程的更多信息。当您访问第二个网址时,浏览器显示的结果是什么?来自Asp WebServer或浏览器错误页面的错误页面?

我想您要提及的错误是由于您忽略了网址中的服务器端口,导致浏览器无法找到您的网站。请注意,在IIS Express上进行调试会为您提供一个随机的Web服务器端口,如果您希望修复此端口,因为http的默认值为80,请使用IIS进行部署。

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

© www.soinside.com 2019 - 2024. All rights reserved.