How to redirect to login page if user directly visits page in ASP.Net MVC

i am developing one application in mvc my problem is after login it will redirect to some page. if i copy url and paste it in another browser its showing error in application but i need to display home page?

I tried this

in Global.asax

 protected void Session_Start(Object sender, EventArgs e)
    {

        Session["UserName"] = null;
        if (HttpContext.Current != null)
        {
            HttpContext.Current.Session.Add("CurrentCompany", "Company 1");
           // HttpContext.Current.Session.Add("Connectionstring", "");
        }
        if (Session["UserName"] == null)
        {
            HttpContext.Current.Session.Add("CurrentCompany", "Company 1");
           // Response.RedirectToRoute("Default");
            HttpContext.Current.Response.RedirectToRoute("Default", new { controller = "Login", action = "Create" });
        }
    }      

But it is not redirecting my be the problem with Response.RedirectToRoute("Default") or HttpContext.Current.Response.RedirectToRoute .

Plz help me

Answers


This will work

protected void Session_Start(Object sender, EventArgs e)
{

    Session["UserName"] = null;
    if (HttpContext.Current != null)
    {
        HttpContext.Current.Session.Add("CurrentCompany", "Company 1");
       // HttpContext.Current.Session.Add("Connectionstring", "");
    }
    if (Session["UserName"] == null)
    {
        HttpContext.Current.Session.Add("CurrentCompany", "Company 1");
        var routeData = new RouteData();
        routeData.Values["controller"] = "Login";
        routeData.Values["action"] = "Create";
        Response.StatusCode = 500;
        IController controller = new HomeController();
        var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
        controller.Execute(rc);
    }
} 

Need Your Help

Making input and output command in programming language

java antlr new-operator printf

so I'm making some basic programming language (just for exercise). I've made simple grammar using ANTLR.

Display Dynamic List of Articles in Joomla

php mysql sql joomla

In a category list, I'd like to be able to display the list of articles dynamically. The website I'm developing allows a user to "submit" an article once they have read it. When they hit the submit

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.