0
votes

Well my site local working well, but when is deployed to production the RedirectToAction fail and show a blank page.

I have tried these Redirect to Action is not working With ASP.NET core

here is the code:

    public ActionResult Login()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Login(Models.UserViewModel person)
    {
        var booleanlog = Models.Security.User.login.DoLogin(person);
        if (booleanlog!=0) {
            ...
            return View("Index");
        }
        else {

            return View("Login");
        }

    }

I already tried this code too and doesn´t work

    [HttpPost]
    public ActionResult Login(Models.UserViewModel person)
    {
        var booleanlog = Models.Security.User.login.DoLogin(person);
        if (booleanlog != 0) {
            var serializeSession = JsonConvert.SerializeObject(booleanlog);
            HttpContext.Session.Set("Rol", Encoding.ASCII.GetBytes(serializeSession));
            return RedirectToAction("Index","Home");
        }
        else {

            return RedirectToAction("Index", "Home");
        }

    }

there are my routes configuration public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseBrowserLink(); app.UseDeveloperExceptionPage(); } else if(env.IsProduction()) {

            app.UseExceptionHandler("/Error");

        } else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();
        app.UseSession();
        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Login}/{id?}");
        });
    }

and show this

redirect to a root locahost and show error 404 not found

in addition, this work in my local VisualStudio IIS Express, My Local IIS, but doesn´t work in my production IIS

1
can you please post the code that you're using?Majdi Saibi
but as I can see in your code, you're not using RedirectToAction.Majdi Saibi
yes, I change to return View to try if work, but same thingChristian José Vallejo
are you using MVC or Razor Pages?Adam Vincent
How are your routes configured? What does HomeController.Index() look like?Valuator

1 Answers

1
votes

You are doing return View(). View() does not return a RedirectToActionResult if you specify a different view. You want to return RedirectToAction() in your else block.


RE: Still doesn't work

Please refer to this document that explains what parameters need to be passed into RedirectToAction()

If you return RedirectToAction('Login') you are telling it to return the result from the action on your current controller.

If this is not the desired behavior, you need to provide what 'Login' is.