I need to define an MVC route for URL like this:
http://localhost/RealSuiteApps/RealHelp/-1/Detail/BRK18482020
where:
Detail - is controller name
default action Index should be executed
-1 is some client id
BRK18482020 is orderId
I need this to go to DetailController, Index action with orderId parameter.
I tried this:
routes.MapRoute(
name: "Detail",
url: "Detail/{id}",
defaults: new { clientid = "-1", controller = "Detail", action = "Index", id = UrlParameter.Optional }
);
but I get a message "Page Not Found". What am I missing here ??
Assuming DetailController action
public ActionResult Index(int clientId, string orderId) { ... }
Then route would be mapped as
routes.MapRoute(
name: "Detail",
url: "{cientId}/Detail/{orderId}",
defaults: new { clientid = "-1", controller = "Detail", action = "Index" }
);
Note that this should also be registered before any default routes.
Related
I want create route config multiple Url. ex: "fr-ca/user/login" and "en-ca/user/login" and its working, but if click submit and proccess data by controller and return RedirectToAction(MVC.User.Dashboard); always return "fr-ca/user/dashboard";
Although I use Url "en-ca/", and always return all link MVC.anything in first is "fr-ca/"
Because in position route config, "fr-ca" is first.
Maybe somebody can help me to solve this, thank u.
routes.MapRoute(
name: "fr-Default",
url: "fr-ca/{controller}/{action}/{id}",
defaults: new { controller = MVC.Content.Name, action = MVC.Content.ActionNames.Show, id = UrlParameter.Optional, siteId = Site.FR.Id },
namespaces: new string[] { "Jay.Cms.Web.Controllers" }
);
routes.MapRoute(
name: "en-Default",
url: "en-ca/{controller}/{action}/{id}",
defaults: new { controller = MVC.Content.Name, action = MVC.Content.ActionNames.Show, id = UrlParameter.Optional, siteId = Site.EN.Id },
namespaces: new string[] { "Jay.Cms.Web.Controllers" }
);
You can write to action attribute in controller
'[Route("fr-ca/{controller}/{action}/{id}")]
[Route("en-ca/{controller}/{action}/{id}")]
public ActionResult Home()
{
return View();
}'
In my web api, I have created this controller:
public class DistributionGroupController : ApiController
{
[HttpGet]
public ServiceResult Index(string id)
{
if (id == null)
return null;
else
return new ServiceResult();
}
}
In addition, this is my route config. I am specifying my default action for my distribution groups route to be "Index":
routes.MapHttpRoute(
"Api action",
"Api/{controller}/{action}"
);
routes.MapHttpRoute(
"Api get",
"Api/{controller}"
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
/*This is the route in question*/
routes.MapHttpRoute(
"DistGroupRoute",
"api/distributiongroup/{id}/{action}",
new { controller = "DistributionGroup", action = "Index" }
);
And in my view, I am using this script to (try to) hit my controller:
$.ajax({
url: "api/distributiongroup/4567bn57n5754",
cache: false,
success: function (response) {
alert('success');
}
});
But my ajax call recieves a 404 Not Found error. However, if I append index to my url from my ajax call, my controller is hit. So, in essence, this does not work:
api/distributiongroup/4567bn57n5754
But this does work:
api/distributiongroup/4567bn57n5754/index
It's my understanding that my default action should get hit if I don't specify my action in my url. What might I be missing here? And, more importantly, how can I make my Index controller get hit when I use a url such as this:
api/distributiongroup/4567bn57n5754
(without specifying the Index action?
routes.MapHttpRoute(
"DistGroupRoute",
"api/distributiongroup/{id}",
new { controller = "DistributionGroup", action = "Index" }
);
instead of:
routes.MapHttpRoute(
"DistGroupRoute",
"api/distributiongroup/{id}/{action}",
new { controller = "DistributionGroup", action = "Index" }
);
There is no need to add the {action} in the route template because you already added it in the defaults object.
Basically, you said:
Whenever there is an URL that matches this route template (api/distributiongroup/{id}/{action}) trigger the Index action in the DistributionGroup controller and pass the id parameter.
This happened because of the order I was specifying my routes in my route config. I was specifying them in this order:
routes.MapHttpRoute(
"Api action",
"Api/{controller}/{action}"
);
routes.MapHttpRoute(
"Api get",
"Api/{controller}"
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapHttpRoute(
"DistGroupRoute",
"api/distributiongroup/{id}/{action}",
new { controller = "DistributionGroup", action = "Index" }
);
Is seems that this route (my default route):
{controller}/{action}/{id}
was overriding my distributiongroups route when I did not specify index in my url. I'm still not entirely sure why this happened. But re-ordering my route configs fixed it. I just needed to put my distributiongroup route before my default route:
routes.MapHttpRoute(
"DistGroupRoute",
"api/distributiongroup/{id}/{action}",
new { controller = "DistributionGroup", action = "Index" }
);
routes.MapHttpRoute(
"Api action",
"Api/{controller}/{action}"
);
routes.MapHttpRoute(
"Api get",
"Api/{controller}"
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I'm coding a website like a platform where we can access the user profile from the following URL:
www.mywebsite.com/DanielVC
The controller that has the details about the profile is the following
Controller: Perfil
Action: Perfil
I already have the following Route for all application:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Pages", action = "Index", id = UrlParameter.Optional }
);
I already tried to create the following route:
routes.MapRoute(
name: "Perfil",
url: "Pages/{id}",
defaults: new { controller = "Pages", action = "Index", id = UrlParameter.Optional }
);
But didn't work
Put this BEFORE (above) the default route.
routes.MapRoute(
name: "Perfil",
url: "{id}",
defaults: new { controller = "Pages", action = "Index", id = UrlParameter.Optional }
);
Also, this will result in every route to be redirected to Perfil route. You must create a redirection in that action if a username is not found (e.g. mywebsite.com/randomuserthatdoesntexist) and/or other routes (mywebsite.com/contact).
EDIT
Example for your method
public class PagesController : Controller
{
public ActionResult Index(string id)
{
if (matchesOtherRoute(id))
RedirectToAction("OtherAction", "OtherController");
if (!userExists(id))
RedirectToAction("NotFoundAction", "ErrorController");
// Do other stuff here
}
}
it should be like
routes.MapRoute(
name: "Perfil",
url: "Pages/{id}",
defaults: new { controller = "Perfil", action = "Perfil", id = UrlParameter.Optional }
);
I have the following action result:
public ActionResult Index(int id, int? siteId)
{
//code here....
return View(object);
}
I have the following route mapping as follows:
routes.MapRoute(
name: "SettingsRoute",
url: "Settings/{id}/{siteId}",
defaults: new
{
controller = "Settings",
action = "Index",
}
);
What do I need to do, so the url will be "Settings?id=1&siteId=133" instead of the format "Settings?id=1" durring initial load. Then to select a site it builds the URl "Settings/1/133".
I am using the following actionLink to create this:
<li>#Html.ActionLink(site.Name, "Index", "Settings", new { id = Model.SettingsEnvironment.EnvironmentID, siteId = site.SiteID }, null)</li>
I can't seem to get the routing down right. Any help would be appreciated. Thanks.
You need to set your optional URL parameter:
routes.MapRoute(
name: "SettingsRoute",
url: "Settings/{id}/{siteId}",
defaults: new
{
controller = "Settings",
action = "Index",
siteId = UrlParameter.Optional
}
);
ref: http://haacked.com/archive/2010/02/12/asp-net-mvc-2-optional-url-parameters.aspx/
My Booking Controller have the following code
public ActionResult Index(string id, string name)
{
return View();
}
and my routeConfig have the below route mappings
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: "Search",
url: "{controller}/{location}/{checkIn}/{checkOut}/{no}",
defaults: new { controller = "Search", action = "Index", location = UrlParameter.Optional, checkIn = UrlParameter.Optional, checkOut = UrlParameter.Optional, no = UrlParameter.Optional }
);
routes.MapRoute(
name: "booking",
url: "{controller}/{action}/{id}/{name}",
defaults: new { controller = "Booking", action = "Index", id = UrlParameter.Optional, name=UrlParameter.Optional }
);}
but when I access the page http://localhost:59041/booking/index/1/libin both params returns null.
see this book
As your application becomes more complex you are likely going to
register multiple routes. When you do this its important that you
consider the order that that you register them. When the routing
engine attempts to locate a matching route, it simply enumerates the
collection of routes and it stops enumerating as soon as it find a
match.
Add a comment This can cause plenty of problems if you’re not
expecting it. Let’s look at an examples where this can be a problem:
routes.MapRoute(
> "generic", // Route name
> "{site}", // URL with parameters
> new { controller = "SiteBuilder", action = "Index" } // Parameter defaults );
>
> routes.MapRoute(
> "admin", // Route name
> "Admin", // URL with parameters
> new { controller = "Admin", action = "Index" } // Parameter defaults );
The snippet above registers two routes. The first route
contains a single placeholder segment and sets the default value of
the controller parameter to SiteBuilder. The second route contains a
single constant segment and sets the default value of the controller
parameter to Admin.
Both of these routes are completely valid, but the order in which they
are mapped may cause unexpected problems because the first route
matches just about any value entered, which means that it will be the
first to match
http://example.com/Admin and since the routing engine stops after
finding the first match, the second route would never get used.
So, be sure to keep this scenario in mind and consider the order in
which you define custom routes.
You should write booking routes at first
routes.MapRoute(
name: "booking",
url: "{controller}/{action}/{id}/{name}",
defaults: new { controller = "Booking", action = "Index", id = UrlParameter.Optional, name=UrlParameter.Optional }
);}
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: "Search",
url: "{controller}/{location}/{checkIn}/{checkOut}/{no}",
defaults: new { controller = "Search", action = "Index", location = UrlParameter.Optional, checkIn = UrlParameter.Optional, checkOut = UrlParameter.Optional, no = UrlParameter.Optional }
);