2014-06-30 6 views
0

Я разрабатываю одно приложение в mvc. Моя проблема после входа в систему будет перенаправлена ​​на некоторую страницу. если я копирую URL-адрес и вставляю его в другой браузер, он показывает ошибку в приложении, но мне нужно отображать домашнюю страницу?Как перенаправить на страницу входа в систему, если пользователь непосредственно посещает страницу в ASP.Net MVC

Я попробовал этот

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

Но это не перенаправлять мой быть проблема с Response.RedirectToRoute ("По умолчанию") или HttpContext.Current.Response.RedirectToRoute.

Plz помочь мне

ответ

1

Это будет работать

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); 
    } 
} 
+0

Thanq вас @Kartikeya Khosla – Karthik

+0

Ваш Добро пожаловать ... –

Смежные вопросы