Restrict user to accesss NON route URL's - ASP.NET MVC - c#

I have a webapplication with the following route:
routes.MapRoute(
name: "Mytest",
url: "Admin/Dashboard/Index",
defaults: new { controller = "Dashboard", action = "Index", ID= "test" }
);
Now when I go using http://localhost:13/Admin/Dashboard/Index it works, but http://localhost:13/Dashboard/Index also working.
But I would like, users can access http://localhost:13/Admin/Dashboard/Index ONLY. I need when users access http://localhost:13/Dashboard/Index should returns 404.

To define route to be ignored use RouteCollectionExtensions.IgnoreRoute method:
public static void RegisterRoutes(RouteCollection routes)
{
// IgnoreRoute() added by default
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute(url: "Dashboard/Index");
routes.MapRoute(
name: "Mytest",
url: "Admin/Dashboard/Index",
defaults: new { controller = "Dashboard", action = "Index", ID = "test" }
);
... additional routes
}

Related

Can't change default action in RouteConfig MVC5

I've a strange behavior of configured route in Route Config in MVC5 app.
So, actually I try to do pretty much easy thing - change default action of a route.
To do that I've changed the defaults of a route, instead of having action = "Index", I've changed it to my required action AddUser (Look at the code below).
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "ManageUser",
url: "{controller}/{action}/{id}",
defaults: new { controller = "ManageUser", action = "AddUser", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
And I've next controller:
[Authorize(Roles ="Admin")]
public class ManageUserController : Controller
{
// GET: ManageUser/AddUser
[HttpGet]
public ActionResult AddUser()
{
return View();
}
}
So, now I expect that whenever user goes by URL: ManageUser/ he'll be redirected to defined default action but it doesn't happen, I just have 404 error. I can fix it simply by adding Index action and then redirect to AddUser, but it doesn't seem right to me. Could somebody help me understand what I've done wrong?
I think, your config should look like this:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"ManageUser",
"ManageUser/{action}/{id}",
new { controller = "ManageUser", action = "AddUser", id = UrlParameter.Optional }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
The first parameter is the name of the route. Second is the URL, which match URLs that start with ManageUser, and allows for other actions in your ManageUser controller. As you can see, it will default to the AddUser action.
And if you want to call AddUser with paramater, you must call it by full url ManageUser/AddUser/1

Is it possible to have more than one routing in ASP.NET MVC?

I want to ask if it's possible to have more than one routing within RouteConfig class. The logic I have is as below, what I want to achieve. I have actionLink("Dashboard", "Account".....) already and want to have a unique one that won't conflict with existing one when page is loaded. Please assist me there is a way.
namespace ContentManagementSystem
{
public class RouteConfig
{
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 } // I already have this working fine
defaults: new { controller = "Dashbaord", action = "_Index", id = UrlParameter.Optional // I want to have separate, but unique route for this controller for actionResult
);
}
}
}
Yes you can have more routes you can :
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Student",
url: "students/{id}",
defaults: new { controller = "Student", action = "Index"}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
As shown in the above code, URL pattern for the student route is students/{id}, which specifies that any URL that starts with domainName/students, must be handled by the StudentController.
Notice that we haven't specified {action} in the URL pattern because we want every URL that starts with student should always use Index action of StudentController. We have specified default controller and action to handle any URL request which starts from domainname/students.
You can read more here
Add it to top of default route:
namespace ContentManagementSystem
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("other_route", "other_route/",
defaults: new { controller = "OtherController", action = "OtherAction" });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } // I already have this working fine
defaults: new { controller = "Dashbaord", action = "_Index", id = UrlParameter.Optional // I want to have seperate, but unique route for this controller for actionResult
);
}
}
}

Retrieve QueryString And Force Custom Url To A Page

I am trying to config custom url in the RouteConfig.cs file as follows with the default one:
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.MapRoute(
name: "Default2",
url: "{controller}/{action}/id/{id}", //Custom url route
defaults: new { controller = "Product", action = "AddImages", id = UrlParameter.Optional }
);
}
The above works fine when I write the followings in the url: 1st routing
http://localhost:1234/Product/AddImages/id/1008
Again, the below also works: 2nd routing
http://localhost:1234/Product/AddImages/1008
But I want to force the url to show or write the url in the address bar as the first one for the specific page 'AddImages' like this: http://localhost:1234/Product/AddImages/id/1008
Is there any way to do it I mean keeping the default routing and for a specific page, the first routing option?
Again, I tried to retrieve the QueryString from the first url as follows:
int id = Convert.ToInt32(Url.RequestContext.RouteData.Values["id"]); //This works for the second routing option
It says - Input string wasn't in a correct format. I guess, this is for the default routing option as it's configured to:
url: "{controller}/{action}/{id}"
Again, is it possible to get the QueryString value from the below url or to config anything:
http://localhost:1234/Product/AddImages/id/1008
In routing table for MVC, the route that are defined first has the highest priority. So all you need is make sure your specific route higher priority than the default one. Just define it earlier. That is it - as mentioned in the comment.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default2",
url: "{controller}/{action}/id/{id}", //Custom url route
defaults: new { controller = "Product", action = "AddImages", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
If you want only the first one then try removing the generic one
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default2",
url: "{controller}/{action}/id/{id}", //Custom url route
defaults: new { controller = "Product", action = "AddImages", id = UrlParameter.Optional }
);
}
If you want it only for that controller only try this -
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default2",
url: "Product/{action}/id/{id}", //Custom url route
defaults: new { action = "AddImages", id = UrlParameter.Optional },
constraints: new { controller = "Product" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
There is a better way to get url parameters. Instead of
int id = Convert.ToInt32(Url.RequestContext.RouteData.Values["id"]); //
try this -
int id = Convert.ToInt32(Request.Params["id"]); //
But if you defined your action method properly, you don't even need to read it from request. It will automatically be mapped.

Mvc 5 supporting legacy ashx links

Is there a way to support ashx links as in MapRoute?
For example I have this
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "ashx",
url: "test/redirect.ashx",
defaults: new { controller = "Click", action = "Redirect" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
This doesnt work but what im looking for is, if the url starts is www.example.com/test/redirect.ashx?foo=bar it will redirect me to my controller ClickController.Redirect(). is this possible? Am i Doing this wrong?

Route Issue ASP.net MVC 4 not redirecting to Index Action

When I try to access to index Page on controller if I use this URL format :
mydomain/taux/index
This works and the Index page is opened. But if I use this url format (I don't add the Index action to URL)
mydomain/taux/
I got "404 not found", but normally it should works and redirect me to index page automatically.
How to fix it please ?
Taux Controller :
// GET: /Taux/
public ActionResult Index()
{
var taux = db.TAUX.Include(t => t.CATEGORIE).Include(t => t.GARANTIE);
return View(taux.ToList());
}
RouteConfig.cs:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
}
}
This is because your default action is set to Login. You need to set it to Index if you want link mydomain/taux/ to redirect to Indext action
If you want to have the redirect just for this specific controller you can use the following route definitions:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Taux",
url: "Taux/{action}/{id}",
defaults: new { controller = "Taux", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
}

Categories

Resources