MapRoute with my extension - asp.net-mvc-4

How to create right MapRoute with my extension? (e.x. "my").
This MapRoute is correct
routes.MapRoute(
name: "Default",
url: "{controller}.my/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
But I want use controller.my?id.
This MapRoute is wrong
routes.MapRoute(
name: "",
url: "{controller}.my/{tid}",
defaults: new { controller = "Home", action = "Index", tid = UrlParameter.Optional }
);

I think that if you add parameters in MapRoute, you can't simply use them as query strings.
Remove those parameters which you want to use them as query strings from MapRoute, and simply use them in action methods:
routes.MapRoute(
name: "",
url: "{controller}.my",
defaults: new { controller = "Home", action = "Index" }
);
Action method:
public ActionResult Index(string tid) // or int tid ...
{
// ...
}
So you can access that by something like ~/Home.my?tid=someThing

Related

MVC route with single level

I need to build a controller action to handle this pattern:
example.com/aString
where aString can be any of a set of arbitrary strings. The controller would cycle thru each of the possible values and, if none match, redirect to a 404.
I'd think it's just a matter of recoding the catch-all but am so far coming up blank. Currently using Sherviniv's suggestion:
//Catchall affiliate shortcuts.
routes.MapRoute(
name: "affLanding",
url: "{query}",
defaults: new
{
controller = "Home",
action = "MatchString"
}
);
Controller:
public ActionResult MatchString(string query)
{
_logger.Info("affLanding: " + query);
return View();
}
If i hard-code my 'search' string into the route.config things work:
routes.MapRoute(
name: "search",
url: "aString",
defaults: new { controller = "home", action = "MatchString"}
);
In routeconfig
routes.MapRoute(
name: "Controller1",
url: "Controller1/{action}/{id}",
defaults: new { controller = "Controller1", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Controller2",
url: "Controller2/{action}/{id}",
defaults: new { controller = "Controller2", action = "Index", id = UrlParameter.Optional }
);
//Other controllers
routes.MapRoute(
name: "search",
url: "{query}",
defaults: new
{
controller = "Home",
action = "MatchString"
}
);
routes.MapRoute(
name: "Default",
url: "",
defaults: new
{
controller = "Home",
action = "Index"
}
);
In your Controller
public ActionResult Index()
{
reutrn view();
}
public ActionResult MatchString(string query)
{
if(Your Condition)
{
//when string query doesnt find
throw new HttpException(404, "Some description");
}
else
{
return view(Your model);
}
}
Remember to add all of your controller's name because if you don't mention them in route config how the server can know it is search parameter or Not.
Hope it helps

Issue in GET route in asp.net mvc razor c#

I have 3 routes total in my code.
If I change the position of 1st with 2nd, then 2nd starts to work but 1st gives 404 error.
Am I doing anything wrong in below code?
This is GET route and works perfectly.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}",
defaults: new {
controller = "Category",
action = "Index",
Category = UrlParameter.Optional
}
);
This is GET route and always give 404 error.
routes.MapRoute(
name: "Default1",
url: "{Category}",
defaults: new {
controller = "Product",
action = "Index",
Category = UrlParameter.Optional
}
);
This is POST route and works perfectly.
routes.MapRoute(
name: "Default_Without_Action",
url: "{controller}/{action}/{Category}",
defaults: new {
controller = "Product",
action = "GetProducts",
Category = UrlParameter.Optional
}
);
test if you put url: "{controller}" and see if this works
routes.MapRoute(
name: "Default1",
url: "{controller}",
defaults: new {
controller = "Product",
action = "Index",
Category = UrlParameter.Optional
}
);
I checked both routes and working fine.
RouteConfig.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//Route -1
routes.MapRoute(
name: "Default1",
url: "{Category}",
defaults: new { controller = "Product", action = "Index", Category = UrlParameter.Optional }
);
//Route - 2
routes.MapRoute(
name: "Default",
//url: "{controller}/{action}/{id}",
//defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
url: "{controller}/{action}/{Category}",
defaults: new { controller = "Category", action = "Index", Category = UrlParameter.Optional }
);
}
On initial/first request it matches the route-1 and calls the Index Action Method of Product Controller as shown in the picture below. Reason - We defined the defaults parameter for controller Products.
If we remove the defaults parameter from route 1 then on initial/first request route-2 URL pattern matches and Index Action method of Category Controller will be called, Reason - We defined the defaults parameter for controller Category.
Browser response with 404 error only when any request doesn't match with the registered URL pattern.

Routing in MVC is not working

I am reading an ebook on MVC.
It have following examples but none of them are working.
routes.MapRoute("", "X{controller}/{action}");
routes.MapRoute("", "Public/{controller}/{action}",
new { controller = "Home", action = "Index" });
Both of them will go to Home controller and index action
Link for First route: http://localhost:14099/XHome/Index
Link for Second route: http://localhost:14099/Public/Home/Index
The output of the both should be the same as per the example in the book. But when i'm trying the same it is not giving any result. i.e resource cannot be found. Am i doing anything wrong here?
Following is my code which i have written:
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute("", "X{controller}/{action}");
routes.MapRoute("MyRoute", "{controller}/{action}",
new { controller = "Home", action = "Index" });
routes.MapRoute("", "Public/{controller}/{action}",
new { controller = "Home", action = "Index" });
}
I was writing the routes after default which is wrong, so i had to write it before the default which is quite obvious.
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute("", "X{controller}/{action}");
routes.MapRoute("MyRoute", "{controller}/{action}",
new { controller = "Home", action = "Index" });
routes.MapRoute("", "Public/{controller}/{action}",
new { controller = "Home", action = "Index" });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}

How to shorten the url to just the action parameter

If a website with url "www.site.com", redirects to HomeController's Index action.
I have
www.site.com/area/controller/action/{nick}
I want the url
www.site.com/{nick}
do the same thing
How I do to create a route and where do I have to create it?
In the RouteConfig.cs or in the AreaRegistration.cs?
In your RouteConfig.cs, add following route after all routes.
routes.MapRoute
(
name: "Nick Route",
url: "{nick}",
defaults: new { area = "AreaName",
controller = "controllerName",
action = "Actionname",
nick = UrlParameter.Optional });
I know that this post is old but if someone is interested, he may find the solution here http://www.nikolay-angelov.eu/custom-url-shortener-in-asp-net-mvc-5/
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "URL",
url: "{shortURL}",
defaults: new { controller = "Home", action = "RedirectToLong", shortUrl = UrlParameter.Optional }
);
routes.MapRoute(
name: "Basic",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", id = UrlParameter.Optional }
);

ASP.NET MVC 4 Routes not working

Route
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Contact",
url: "Contact",
defaults: new { controller = "Home", action = "Contact" }
);
my controller
public class HomeController : BaseController
{
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
My Global.asax
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
}
Finally my request url
http://localhost:1234/Contact/
Error on browser
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its
dependencies) could have been removed, had its name changed, or is
temporarily unavailable. Please review the following URL and make
sure that it is spelled correctly.
Requested URL: /Contact/
Version Information: Microsoft .NET Framework Version:4.0.30319;
ASP.NET Version:4.0.30319.18033
What am I doing wrong?
Solution:
Custom route should take the precedence
routes.MapRoute(
name: "Contact",
url: "Contact",
defaults: new { controller = "Home", action = "Contact" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
The Framework always tries to match the URL of the request to a route in the order of the Routes added to the RouteCollection
So you should put the custom routes before the default route,
//Custom route
routes.MapRoute(
name: "Contact",
url: "Contact",
defaults: new { controller = "Home", action = "Contact" }
);
//default route
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
You can use:
routes.MapRoute(
name: "Default",
url: "{*p}",
defaults: new { controller = "Home", action = "Index", p = UrlParameter.Optional }
);
The asterisk indicates that it's a catch-all route. Keep in mind that these routes are extremely greedy, make sure that this stays below any specific routes.
You could also add a route constraint to this route which can determine whether the page exists in the database or something.