J. Doe
J. Doe

Reputation: 91

Strange redirect in MVC

I have MVC app with login functionality and I have several empty controlls, for example: Home. Home Controller does not contain anything at all. The View for the Home controller is a default Home View created by Visual Studio. And there is a strange thing: When I run my web app, it starts from Account/Login But if I click on the menu (or even if I type the path to Home/Index) it automatically redirects me back to Account/Login If I type Account/SignUp - it works fine. But if I go to any View of my empty controllers I get this strange redirect. I have not had this problem in the other projects. So I'm a bit stuck. Please advise.

Upvotes: 1

Views: 89

Answers (1)

NightOwl888
NightOwl888

Reputation: 56859

Actually, as a best practice you should have the AuthorizeAttribute applied to the whole site. It works best to do that in your FilterConfig file.

public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
        filters.Add(new AuthorizeAttribute());
    }
}

Then you should use the AllowAnonymous attribute to selectively make parts of your site available without logging in (such as the home page).

public class HomeController
{
    [AllowAnonymous]
    public ActionResult Index()
    {
        return View();
    }
}

See this article for details.

Upvotes: 1

Related Questions