重定向到另一个视图MVC WebApi.net

本文关键字:WebApi net MVC 视图 另一个 重定向 | 更新日期: 2023-09-27 18:16:26

我使用MVC WebApi创建身份验证,但当用户身份验证时,我试图将其发送到另一个视图,但由于某种原因它不工作,我不知道为什么

RoutConfig:

 public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Routing", action = "LogIn", id = UrlParameter.Optional }
        );
    }
}
控制器代码:

public class RoutingController : Controller
{
    //
    // GET: /Routing/
    public ActionResult Index()
    {
        return View();
    }
    public ActionResult Projects()
    {
        return View();
    }
    public ActionResult Users()
    {
        return View();
    }
    public ActionResult LogIn()
    {
        return View();
    }
    [HttpPost]
    public ActionResult LogInPost(string userName, string password)
    {
        User user = new User();
        RoleByUser rByU = new RoleByUser();
        password = UserController.EncriptPassword(password);
        var checkUser = user.Get(userName);
        var userExists = (from userInList in checkUser where userInList.UserName == userName && userInList.Password == password select userInList).FirstOrDefault();
        if(userExists!= null)
        {
            var roles = (from roleByUser in userExists.listOfRole select roleByUser.RoleName.Trim()).ToArray();
            IPrincipal principal = new GenericPrincipal(
            new GenericIdentity(userExists.UserName), roles);
            SetPrincipal(principal);
        }
        //Users();
        return RedirectToRoute("Users");
    }
    private void SetPrincipal(IPrincipal principal)
    {
        Thread.CurrentPrincipal = principal;
        if (System.Web.HttpContext.Current != null)
        {
            System.Web.HttpContext.Current.User = principal;
        }
    }
}

视图代码:

@{
    ViewBag.Title = "LogIn";
}
<link href="~/css/Style.css" rel="stylesheet" type="text/css" />
<div class="container">
    <div class="card card-container">
        <img id="STK" class="profile-img-card" src="Images/Softtek.png" />
        <p id="profile-name" class="profile-name-card"></p>
        <form class="form-signin">
            <span id="reauth-email" class="reauth-email"></span>
            <input type="text" id="txtUserName" class="form-control" placeholder="Email address" required autofocus />
            <input type="password" id="txtPassword" class="form-control" placeholder="Password" required />
            <div id="remember" class="checkbox">
                <label>
                    <input type="checkbox" value="remember-me" /> Remember me
                </label>
            </div>
            <button id="btnLogIn" type="submit" class="btn btn-lg btn-primary btn-block btn-signin"  >Sing In</button>
        </form><!-- /form -->
        <a href="#" class="forgot-password">
            Forgot the password?
        </a>
    </div><!-- /card-container -->
</div><!-- /container -->

<script>
$(document).ready(function () {
    $('#btnLogIn').click(logIn);
});
function logIn() {
    $.ajax({
        type: "POST",
        url: "http://localhost:21294/Routing/LogInPost",
        dataType: "json",
        data: { userName: $('#txtUserName').val(), password: $('#txtPassword').val() },
        success: function () {
            toastr.success('Tus datos fueron agregados exitosamente!');
        },
        error: function (err, e, error) {
            toastr.error('Error')
        }
    });
}
</script>

重定向到另一个视图MVC WebApi.net

您注册的唯一路由名是Default

尝试重定向到action:

return RedirectToAction("Users");