Reputation: 289
After I run my program which is in MVC, the url it goes is Home/Index. Where to change this? I want to check if the user is logged in, to redirect so some other page. If he isn't logged in, then the url can be Home/Index.
Upvotes: 0
Views: 823
Reputation: 22857
If you are using MVC you should look at using the Authorize action filter
The url you go to on not being authenticated is set in the web.config if you are using forms authentication.
Upvotes: 3
Reputation: 19465
You sort of asking two things.
You application automatically goes to Home/Index
because of this, you'll find the below code if you double click your Global.asax
file.
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Change the "Home" and "Index" strings for your custom default.
Now, for your login requirement, you could leave the default route and do this:
public class HomeController
{
public ActionResult Index()
{
if(!Request.IsAuthenticated)//if NOT authenticated
{//go somewhere else
return RedirectToAction(actioName, controllertName);
}
//for logged in users
return View();
}
}
Upvotes: 0
Reputation:
For the first part of your question (the route), check out the default route, its usually set to
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Which is in your Global.asax file in the web application, and this is why you are seeing what you are seeing.
You really need to read up on ASP.Net Routing - http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/asp-net-mvc-routing-overview-cs
Upvotes: 0