Routing Url that has no action name - asp.net-mvc-3

How to route this url
http://localhost:1986/en/Hotel/#forward
to
controller = "Hotel",
action = "Results"
Since I need to refresh this page when the user sign in from this page, I need to redirect to the above said controller/action.
Please help

I have found some solution my self
routes.MapRoute(
"HotelResultAfterFlightSelection1", // Route name
"Hotel/Results/{tripid}", // URL with parameters
new
{
controller = "Hotel",
action = "Results",
tripid = UrlParameter.Optional
}
);
routes.MapRoute(
"HotelResultAfterFlightSelection2", // Route name
"Hotel", // URL with parameters
new
{
controller = "Hotel",
action = "Results"
}
);

Related

Can't map route to action mvc3

I'm trying to create a new Route in MVC3 to achieve the link http://localhost/Product/1/abcxyz:
routes.MapRoute(
"ProductIndex", // Route name
"{controller}/{id}/{name}", // URL with parameters
new { controller = "Product", action = "Index", id = UrlParameter.Optional, name = UrlParameter.Optional } // Parameter defaults
);
And I used Route Link like this:
<li>#Html.RouteLink("My Link", "ProductIndex", new { controller = "Product", id = 10, name = "abcxyz" })</li>
Product Index action:
public ViewResult Index(int id, string name)
{
var product = db.Product.Include(t => t.SubCategory).Where(s => s.SubID == id);
return View(product.ToList());
}
The url render as I expected. But when I click on it, I got a 404 error with message
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
UPDATE
I place that Route above Default Route and the URL work fine. But there's a problem occure. My index page http://locahost point directly to Index action of Product controller, but I want it points to Index action of Home controller instead
It's because you have 2 optional parameters in your route and the engine can't work out which one to set the value to. See my answer to a similar issue here
You can create a specific route for your products controller first (with mandatory id) and then have the generic fallback route afterwards.
routes.MapRoute(
"ProductIndex", // Route name
"products/{id}/{name}", // URL with parameters
new { controller = "Product", action = "Index", name = UrlParameter.Optional } // Parameter defaults
);
Try it
routes.MapRoute(
"Default", // Route name
"{controller}/{id}/{name}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional, name = UrlParameter.Optional } // Parameter defaults
);
for routing details see this link. In this link every type of routing is discussed.
http://www.codeproject.com/Articles/408227/Routing-in-MVC3

MVC3 :: Using global.asax to direct a specific controller/action to a different controller/action

In an MVC3 app I have a view in my home controller that is accessed via the following URL:
http://mysite.com/home/mycontent
However, my user wants to accees it via:
http://mysite.com/mycontent (without a specific reference to the home controller)
I've tried modifying my global.asax with the following:
routes.MapRoute(
"MyContent", // Route name
"MyContent", // URL with parameters
new { controller = "Home", action = "MyContent"} // Parameter defaults
);
But I get 404 errors when I try to use it. Is there a way I can accomplish what my user is wanting to do - redirecting a URL to a specific controller/view pair?
I think you should keep some "parameters":
routes.MapRoute(
"MyContent", // Route name
"{action}/{id}", // URL with parameters
new { controller = "Home", action = "MyContent", id = ""} // Parameter defaults
);
In what order are your routes registered? That route should come before your default route.
E.g.
routes.MapRoute(
"MyContent", // Route name
"MyContent", // URL with parameters
new { controller = "Home", action = "MyContent"} // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Pages", action = "Index", id = UrlParameter.Optional }
);
Otherwise, it should work... I have routes defined exactly the same as that.

asp.net mvc routing - Multiple Parameters

I've just started MVC and I can pass through an ID to a page but can't seem to get my routing to work with two parameters. Does anyone have any ideas why?
Here is my code:
Global:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
routes.MapRoute(
"EditVoucher", // Route name
"{controller}/{action}/{id}/{userid}", // URL with parameters
new { controller = "Admin", action = "EditVoucher", id = "", userid = "" } // Parameter defaults
);
**My controller:**
[HttpGet]
public ActionResult EditVoucher(int ID, int UserID)
{
}
**my link:**
#Html.ActionLink("[Edit]", "EditVoucher", new { Controller = "Admin", id = item.ID, userid = 2 })
this passes through the values fine but I end up with this sort of URL:
**/Admin/EditVoucher/2?userid=2**
thanks
ActionLink will use the first route that can satisfy your parameters.
Since the first (default) route also satisfies your parameters, you need to put the custom route first.

Can not find the project? Redirects to the next route

I have a project that can be accessed at the URL:
myapp.com/project/my-project
Route
To be able to use this route, I recorded the following in my Global.asax
routes.MapRoute(
"Project", // Route name
"Project/{url}/{action}", // URL with parameters
new { controller = "Project", action = "Details" } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Action
When you browse the URL ´myapp.com/project/create´, I would like to ´create´ action is called, not my ´details´ action.
But the URL myapp.com/project/my-project should usually be interpreted by the details action.
public ActionResult Details(string projectUrl)
{
var project = _projectService.Get(projectUrl);
if (project == null)
//Not found! Redirect to next route!
var model = Mapper.Map<ProjectViewModel>(project);
return View(model);
}
Question
How to make, Create, Delete, Edit to be interpreted by the default route?
Here is the create route with your existing route:
routes.MapRoute(
"ProjectCreate", // Route name
"project/create", // URL with parameters
new { controller = "Project", action = "Create" } // Parameter defaults
);
routes.MapRoute(
"Project", // Route name
"project/{url}/{action}", // URL with parameters
new { controller = "Project", action = "Details" } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Your project url parameter name should be url not projectUrl as that is the name of the route value.
Here is my ProjectController:
public ActionResult Details(string url)
{
return View();
}
public ActionResult Create()
{
return View();
}
The appropriate action is hit when I go to either of these URLs:
/project/create
/project/test
/project/test/details
And pass "test" as the url value.
Its worth noting that routes are processed in the order they are added, so you need to make sure they go in order from most specific to most general like you have, and not reverse that.

ASP.NET MVC 3.0 Url Rewritng

I have Action called Contact in home controller
<mysite>/Home/Contact
I want to be able by typing <mysite>/Contact to get the same result as <mysite>/Home/Contact
Is it possible to do with mvc 3.0 routes or RouteMagic?
Currently i am trying to achieve this like that, but no luck:
Custom Routes:
routes.MapRoute(
"Contact", // Route name
"Contact", // URL with parameters
new { controller = "Home", action = "Contact", id = UrlParameter.Optional } // Parameter defaults
);
RouteMagic:
var route = routes.MapRoute("new", "Contact");
routes.Redirect(r => r.MapRoute("old", "Home/Contact"))
.To(route);
Update
Ok the custom routes should be defined first, now it is working(in case of custom routes), but there is appeared a new question why route magic returning error:
Server Error in '/' Application.
Value cannot be null or empty.
Parameter name: controllerName
Make sure your new route occurs before the default route (since it will match as well) when defining the route.
routes.MapRoute(
"Contact", // Route name
"contact", // URL with parameters
new { controller = "Home", action = "Contact", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Contact", id = UrlParameter.Optional } // Parameter defaults
);
have you tried the rewrite module in iis7 ?
its easy to use , donwlod it from here:
http://learn.iis.net/page.aspx/460/using-the-url-rewrite-module/

Resources