Mvc 5 supporting legacy ashx links - c#

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?

Related

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

Hiding Action name from URL

My url is this,
localhost:45678/Blogs/Index
I need it like this format
localhost:45678/Blogs
i write below code in route file but,
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Blog",
url: "{Blog}",
defaults: new { controller = "Blog", action = "Index" }
);
routes.MapRoute(
name: "forblogcontent",
url: "Blog/{slug}/{id}",
defaults: new
{
controller = "Blog",
action = "DisplayContent",
slug = UrlParameter.Optional
}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Content", action = "AddContent", id = UrlParameter.Optional }
);
}
When i click on an link button it still shows
localhost:45678/Blogs/Index
My link button code is
Blogs
How to correct this?

Remove action from url in asp.net mvc razor c#

I have a url like this:
http://localhost:57482/ModuleName/Index/Value
I want to change it like this: http://localhost:57482/ModuleName/Value
Code in RouteConfig
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{Category}",
defaults: new { controller = "Product", action = "Index", Category = UrlParameter.Optional }
);
}
Am I doing anything wrong?
You are specifying your URL as being of the format {controller}/{action}/{Category}, and in the next line you say action = "Index". So, instead of:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{Category}",
defaults: new { controller = "Product", action = "Index", Category = UrlParameter.Optional }
);
}
try:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{Category}",
defaults: new { controller = "Product", action = "", Category = UrlParameter.Optional }
);
}
Not sure if you need the action = "" part, so you might as well try omitting it

Issue related to routing in MVC

I have MVC webapplication. i have written following rules in Route.config file
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: "about",
url: "Home/About-WDI",
defaults: new { controller = "Home", action = "About"}
);
}
and in global.asax file, i have written following code:
RouteConfig.RegisterRoutes(RouteTable.Routes);
now problem is when i type http://localhost:60123/Home/About-WDI
it gives me error saying Resources not found
what is issue in above code?
Thanks
As already mentioned in the comments, the order of the routes is taken into concern. And as your {id} parameter is optional the URL will be matched by the first route. So simply exchange both routes:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "about",
url: "Home/About-WDI",
defaults: new { controller = "Home", action = "About"}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}

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.

Categories

Resources