I want to set MVC route - model-view-controller

I want to set routing, when the user will write a URL like www.abc.com/param1, it's auto-redirect or call to
www.abc.com/itemview/index/param1
www.abc.com/itemview/index/ is a fixed part.
I tried with the below route but it was not worked?
routes.MapRoute(name: "Default",
url: "ItemsViewNew/Index/{id}",
defaults: new { controller = "ItemsViewNew", action = "Index", id = UrlParameter.Optional }
);

Related

Url.Action and Url.RouteUrl renders empty on production box(MVC3)

I am having a strange issue. The Url.Action and Url.RouteUrl renders correctly in the development server(IIS 7.5, Windows 7) but the same code on production box(dedicated server running IIS 7.5, Win 2008 R2) renders it blank.
The html in my view is :
<div style="display:none;">
</div>
The output in view source is:
<div style="display:none;">
</div>
The difference between dev and prod environment is that on my dev machine mvc4 is installed and on prod box only mvc3 is installed.
I have tried routeDebug on production box. When I browse for home/generateCaptcha the "default" route is selected.
I do not understand why the url.action is not able to generate the correct url only on production box.
While deploying, I have even added the deployable dependencies.
I have the following routes defined:
routes.MapRoute(
name: "ByCatSubCat" // Route name
, url: "browse/{categoryName}/{subcategoryName}/" // URL with parameters
, defaults: new { controller="Greeting", action="List"} // Parameter defaults
);
routes.MapRoute(
name: "CardDetail" // Route name
, url: "browse/{categoryName}/{subcategoryName}/card{id}" // URL with parameters
, defaults: new { controller = "Greeting", action = "Details" } // Parameter defaults
);
routes.MapRoute(
name: "ByCatSubCatAll" // Route name
, url: "browse/{categoryName}/{subcategoryName}/" // URL with parameters
, defaults: new { controller = "Greeting", action = "List", subcategoryName="all" } // Parameter defaults
);
routes.MapRoute(
name: "ByCat" // Route name
, url: "browse/{categoryName}/" // URL with parameters
, defaults: new { controller = "Greeting", action = "List" } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}/", // URL with parameters
new { controller = "Greeting", action = "List", id = UrlParameter.Optional } // Parameter defaults
);
Please help.
Update:
It seems that UrlParameter.Optional does not work here.
When I added one more route without having UrlParameter.Optional, all the links started working in production box.
My updated routes are:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "ByCatSubCat" // Route name
, url: "browse/{categoryName}/{subcategoryName}/" // URL with parameters
, defaults: new { controller = "Greeting", action = "List" } // Parameter defaults
);
routes.MapRoute(
name: "CardDetail" // Route name
, url: "browse/{categoryName}/{subcategoryName}/card{id}" // URL with parameters
, defaults: new { controller = "Greeting", action = "Details" } // Parameter defaults
);
routes.MapRoute(
name: "ByCatSubCatAll" // Route name
, url: "browse/{categoryName}/{subcategoryName}/" // URL with parameters
, defaults: new { controller = "Greeting", action = "List", subcategoryName = "all" } // Parameter defaults
);
routes.MapRoute(
name: "ByCat" // Route name
, url: "browse/{categoryName}/" // URL with parameters
, defaults: new { controller = "Greeting", action = "List" } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/", // URL with parameters
new { controller = "Greeting", action = "List" } // Parameter defaults
);
routes.MapRoute(
"Default1", // Route name
"{controller}/{action}/{id}/", // URL with parameters
new { controller = "Greeting", action = "List", id = UrlParameter.Optional } // Parameter defaults
);
You need to set up an area, for example: Url.Action("Models", "Russian", new { brand = mod.Brand.NameEngShort, area = "Catalog" })

MVC3 Routes - how to ensure a route with a specific value takes precedence?

Given the following two defined routes:
routes.MapRoute(name: "CityCategoryPage", url: "{city}-{state}/{categoryName}/__c/", defaults: new { controller = "Home", action = "GeoSubCategories" });
routes.MapRoute(name: "CityStateCategoryResults", url: "{city}-{state}/{categoryName}/{searchTerm}/{pageNumber}/{pageSize}", defaults: new { controller = "Results", action = "SearchCityStateCategory", pageNumber = UrlParameter.Optional, pageSize = UrlParameter.Optional });
If I remove the second route, I get the expected action of seeing the results on the home page. However with the second route present, I'm always forwarded to a results page.
I have an idea as to why this is happening: "_c" is still seen as an optional parameter which matches the optional condition for the second route, but not sure how to get this to work. I would prefer not to append the "_c" to the URL - also wondering if there is another way around this?
Thanks.
If I understand you correctly you want the frontpage to be displayed if no search term is provided?
If so, try to match the route with the optional searchTerm first, and default to the home page route if no searchTerm is present, like so:
routes.MapRoute(name: "CityStateCategoryResults", url: "{city}-{state}/{categoryName}/{searchTerm}/{pageNumber}/{pageSize}", defaults: new { controller = "Results", action = "SearchCityStateCategory", pageNumber = UrlParameter.Optional, pageSize = UrlParameter.Optional });
routes.MapRoute(name: "CityCategoryPage", url: "{city}-{state}/{categoryName}/", defaults: new { controller = "Home", action = "GeoSubCategories" });

MVC 3 - Simplifying routing: How do I? multiple URL matches in a single MapRoute

In my Global.asax.cs routing I have a bunch of routing entries like this:
routes.MapRoute(
name: "About",
url: "about",
defaults: new { controller = "Home", action = "About" }
);
routes.MapRoute(
name: "Buy",
url: "buy",
defaults: new { controller = "Home", action = "Buy"}
);
routes.MapRoute(
name: "Blog",
url: "blog",
defaults: new { controller = "Home", action = "Blog"}
);
And so on - I could have facebook, twitter and so on, so that when people access
http://www.mywebsite.com/blog or /twitter or /facebook etc. I redirect them to the actual links.
Am I missing something obvious or is there a simpler way to combine all these in a single entry?
This is what route parameters are for:
routes.MapRoute("Default", "{action}", new { controller = "home" });
Of course, this will catch any single word URL and then complain if that action doesn't exist in the home controller. You can map other routes before this one and they will take precedence.

Simple Html.Actionlink question (MVC3)

The Route:
routes.MapRoute(
"Items", // Route name
"{controller}/{action}/{id}", // URL with parameters
new {controller = "Item", action = "Index", id = UrlParameter.Optional} // Parameter defaults
);
The htmlhelper:
#Html.ActionLink("Chairs", "List", "Item", new {id="Chairs"}, null)
The link it generates:
http://localhost:57899/Item/List?id=Chairs
What I want it to show:
http://localhost:57899/Item/List/Chairs
How to do that?
Instead of using ActionLink what happens if you try the following?
#Html.RouteLink("Items", new { id = "Chairs" })
You call Html.RouteLink (not Action Link) and map an additional route under your generic like this:
routes.MapRoute(
"ChairsRoute", // Route name
"Item/List/{id}", // URL with parameters
new {controller = "Item", action = "Index", id = UrlParameter.Optional} // Parameter defaults
);
when you call RouteLink, you'll simply pass that "ChairsRoute" name

Configure asp.net mvc hocalhost/Home/Products to hocalhost/Products

How to configure asp.net mvc routing to get
hocalhost/Products and hocalhost/Search
instead of
hocalhost/Home/Products and hocalhost/Home/Search
i.e. to remove Controller name from the route?
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Products", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default",
"{action}/{id}",
new { controller = "Home", action = "Products", id = UrlParameter.Optional }
);
Thus:
http://example.com/ => controller=Home, action=Products
http://example.com/Products => contoller=Home, action=Products
http://example.com/Search => contoller=Home, action=Search

Resources