I'm new to MVC Framework; I'm trying to learn.
I'm trying to Redirect from Login to Home Page
[HttpPost]
public ActionResult Login(Register model)
{
StudentDBHandle db = new StudentDBHandle();
DataTable dst = db.Login(model);
if (dst.Rows.Count==1)
return RedirectToAction("Index","Student");
else
{
ViewBag.Message = "Login Failed";
return View();
}
}
it says "No route in the route table matches the supplied values."
So I Added Route in routeconfig file
public static void RegisterRoutes(RouteCollection routes
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Login",
url: "{controller}",
defaults: new { controller = "Register", action = "Login", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "HomePage",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Student", action = "Index", id = UrlParameter.Optional }
);
}
When the login succeeds, it redirects to the same Login page
I have changed the order, but it's not working.
Related
I have a website developed in dot net 2.0 I need to update that in to MVC framework. How to keep my old URLs same in MVC? For eg. WWW.something.com/Home.aspx and WWW.something.com/About.aspx. I need to keep this URL structure. Please help.
Use MVC routing to make your URLs the same as your old site.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Home",
url: "Home.aspx",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "About",
url: "About.aspx",
defaults: new { controller = "Home", action = "About" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Note that in MVC 5 you could use attribute routing as an alternative to putting every route in the shared RegisterRoutes method.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// Important: Put this before your default route
// or attribute routing won't work.
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
public class HomeController : Controller
{
[Route("Home.aspx", Name = "Home")]
public ActionResult Index()
{
return View();
}
[Route("About.aspx", Name = "About")]
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
}
Another alternative would be just to use the new URL scheme and use the IIS rewrite module to configure 301 redirects from your old URL locations to your new ones so you don't lose any traffic and all of your old hyperlinks will still work.
I am using mvc4 framework and .net framwork 4.5. I need a url like this:
www.examples.com/name (note: 'name' will be change dynamically)
which routes to the same page.
I have tried like this but getting error
My action method is like this:
public ActionResult Userlist(string status)
{
return View();
}
Route Config
routes.MapRoute(
"user",
"{status}",
new { controller = "Home", action = "Userlist" }
);
How can I create a route syntax and redirect this to a controller?
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// Add this route config
routes.MapRoute(
name: "Default_Userlist",
url: "{status}",
defaults: new { controller = "Home", action = "Userlist" },
namespaces: new[] { "MyMvcProject.Controllers" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "MyMvcProject.Controllers" }
);
}
Don't forget to replace the "MyMvcProject" with the name of your project.
Issue
This is a bad practice because the value of the status could easily cause conflicts with your action methods.
I am using ASP.NET MVC5, my routing was working fine until i create new controller Administrator with Action CreateRole. previously i have modified routes in RouteConfig for my Dashboard, as i don't want to take controller name but just Action name. I have tried with Administrator controller but i still getting error "The resource cannot be found."
many thanks in advance
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//======In order to use only Action Title in URL======//
//---Dashboard Home Route--//
routes.MapRoute(
"Home",
"Home/{id}",
new { controller = "Dashboard", action = "Home", id = UrlParameter.Optional }
);
//**************************Administrator Route***************************//
routes.MapRoute(
"CreateRole",
"CreateRole/{id}",
new { controller = "Administrator", action = "CreateRole", id = UrlParameter.Optional }
);
//========default routing=======//
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
}
Controller
public class AdministratorController : Controller
{
public ActionResult CreateRole()
{
return View();
}
}
I have a controller named Registration and an action method in it as the following:
public JsonResult GetReqs(GridSettings gridSettings, int rt)
{
...//whatever
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
So I added a route and now my RouteConfig.cs is like this:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "RegReq",
url: "{Registration}/{GetReqs}/{rt}",
defaults: new { controller = "Registration", action = "GetReqs", rt = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
However I can't access the rt parameter and the action method GetReqs isn't called(I set a breakpoint on it but nothing happened). Where is the mistake?
Edit: Link example I tried : ~/Registration/GetReqs/1
I think you need to remove the brackets in your first route:
routes.MapRoute(
name: "RegReq",
url: "Registration/GetReqs/{rt}",
defaults: new { controller = "Registration", action = "GetReqs",
rt = UrlParameter.Optional }
);
The default route has {controller} to tell MVC to use the string in that section of the url as the controller name. You know the controller, so you just need to match the specific string.
I am trying to show user details at the following url :
www.website.com/users/yasser
where the last entry yasser is the username I have tried a couple of routes but it just does nt work.
My User controller is as shown below.
public class UserController : Controller
{
public ActionResult Index(string username)
{
var model = _service.GetUserDetails(username);
return View(model);
}
}
I have reffered this and couple of other links, but I really could not figure out how it worked.
Can some one help me out on this. Thanks
Edit :
My current route config is below
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Routes executes from top to the bottom:
routes.MapRoute("UserProfile",
"Users/{username}",
new { controller = "User", action = "Index", username = string.Empty }
);
routes.MapRoute("Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional}
);