I have a URL request in the following format: http://localhost/activate?activationCode=X
I would like this request to be handled in the Home controller, by the Activate action.
I am not sure how to proceed. I have looked at RouteConfig.cs and see the way routes are defined here:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
But how can I make Home/Activate handle URL's of the following format? http://localhost/activate?activationCode=X
...so as to add a special case where activate proceeding the host name goes to the Home controller, and Activate action?
You need to create a specific route
routes.MapRoute(
name: "Activate",
url: "Activate/{id}",
defaults: new { controller = "Home", action = "Activate", id = UrlParameter.Optional }
);
and place this one before the default route.
In addition if you want http://localhost/activate/X rather than http://localhost/activate?activationCode=X, then change it to
routes.MapRoute(
name: "Activate",
url: "Activate/{activationCode}",
defaults: new { controller = "Home", action = "Activate", activationCode = UrlParameter.Optional }
);
and make the method in HomeController
public ActionResult Activate(string activationCode)
Related
I am having an issue while dealing with routing in MVC.
I have defined the following routes in Route.Config
routes.MapRoute(
name: "Test",
url: "{controller}/{action}/{param}",
defaults: new { controller = "Home", action = "FirstAction" }
);
routes.MapRoute(
name: "Testy",
url: "{controller}/{action}/{secondparm}",
defaults: new { controller = "Home", action = "SecondAction" }
);
routes.MapRoute(
name: "Test2",
url: "{controller}/{action}/{encodedparam}",
defaults: new { controller = "User", action = "UserInfo" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Problem is that the first route is working fine but in second and third I got null values for the respective parameter.
Did I miss something?
Thanks in advance
You must match the parameter name for the third. If you write :
{id}
You must write
public ActionResult AnyAction(int id)
id could be of any type
i think there is conflict beetween your routes. You need only this mapping :
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
if you need custom routing for other elements don't put "{controller}/{action}/..." inside, because he will take the first route config that is matching with.
You can call all your routes precising the name of your parameter if its different from "id" :
http://localhost/home/firstaction?param=123
http://localhost/home/secondaction?secondparam=123
http://localhost/user/userinfo?encodedparam=123
As you want to use the URL in such way: localhost:portnumber/home/secondaction/value instead of localhost:portnumber/home/secondaction?secondparam=value.
You have to follow the below approach, which would behave as generic for all your Action Methods, whether they are containing parameters or not.
Declare default route in RouterConfig.cs as:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Then Parameter name in method must be same as in default route:
public ActionResult SecondAction(string id)
{
return View();
}
Paramter type could be any type.
In this way you do not need to declare other routes.
In ASP.NET MVC route configuration is like below:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I am using [Route] attribute in Controller:
[Route("login")]
public ActionResult Login()
{
return View();
}
I can go to the page using both /login and Auth/Login. The second link is clearly from the default route, but I don't want that URL to go to my login page.
How can I do that?
To block a route from being served, you can use IgnoreRoute as long as you place it before the route you want to block:
routes.IgnoreRoute(url: "Auth/Login");
// Register any custom routes here...
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
How do I set Default Controller for my ASP.NET MVC 4 project without making it HomeController?
How should I setup a default Area when the application starts?
the best way is to change your route. The default route (defined in your App_Start) sets /Home/Index
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters*
new { controller = "Home", action = "Index",
id = UrlParameter.Optional }
);
as the default landing page. You can change that to be any route you wish.
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters*
new { controller = "Sales", action = "ProjectionReport",
id = UrlParameter.Optional }
);
Set below code in RouteConfig.cs in App_Start folder
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional });
}
IF still not working then do below steps
Second Way :
You simple follow below steps,
1) Right click on your Project
2) Select Properties
3) Select Web option and then Select Specific Page (Controller/View) and then set your login page
Here, Account is my controller and Login is my action method (saved in Account Controller)
Please take a look attached screenshot.
I didn't see this question answered:
How should I setup a default Area when the application starts?
So, here is how you can set up a default Area:
var route = routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
).DataTokens = new RouteValueDictionary(new { area = "MyArea" });
In case you have only one controller and you want to access every action on root you can skip controller name like this
routes.MapRoute(
"Default",
"{action}/{id}",
new { controller = "Home", action = "Index",
id = UrlParameter.Optional }
);
I have the following routings in my RoutingConfig.cs
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
// Not working why?
routes.MapRoute(
name: "AdminLoginRequestUrl",
url: "{controller}/{action}/{requestUrl}",
defaults: new { controller = "Admin", action = "Login", requestUrl= UrlParameter.Optional }
);
The problem is that the second routing is not working
What do i miss here? does someone has any tips or Ideas MVC is abit new for me
You cannot create different route only by differentiating parameter name, both route present similar one. Also move your custom route above default one. You can try this
routes.MapRoute(
name: "AdminLoginRequestUrl",
url: "{controller}/{action}/route2/{requestUrl}",
defaults: new { controller = "Admin", action = "Login", requestUrl= UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
you can use anything insted of route2 to differentiate from default route
You cannot differ your routs by variable names. You can create more specific route like this:
routes.MapRoute(
name: "AdminLoginRequestUrl",
url: "Admin/{action}/{requestUrl}",
defaults: new { controller = "Admin", action = "Login", requestUrl= UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Note that the order of registration is significant.
Or you can rename your parameters to something that will be appropriate for both controllers and use that.
Consider using MapHttpAttributeRoutes(), you can read about it here: https://blogs.msdn.microsoft.com/webdev/2013/10/17/attribute-routing-in-asp-net-mvc-5/1.
Edit: Changed link for better article.
Then you can use attributes to define more specific rules on your actions.
Example:
public class Admin : Controller
{
[Route("admin/{requestUrl}")]
public ActionResult Login(string requestUrl) { ... }
}
I have the following controller:
public class MyController : BaseController
{
public ActionResult Index(string id) { /* Code */ }
public ActionResult MyAjaxCall(string someParameter) { /* Code */ }
}
I have also added the following in the RouteConfig.cs
routes.MapRoute(
name: "MyController",
url: "MyController/{id}",
defaults: new { controller = "MyController", action = "Index" }
)
So my idea is to be able to go directly to the index action using this url /MyController/{Id}, and that seems to work.
However when on the Index page I need to make an Ajax call to /MyController/MyAjaxCall/{someParameter}. However this url is pointing to the Index controller, and is interpreting MyAjaxCall as the id in the Index action.
Any ideas how I can exclude this action from following the newly added route config setting?
If that your id can only be integer number, you can add a constraint to your id field, which specifies that your id can only be numbers:
routes.MapRoute(
name: "MyController",
url: "MyController/{id}",
defaults: new { controller = "MyController", action = "Index" },
constraints: new { id = #"\d+" } // <- constraints of your parameters
)
Here you can use any regular expression that works for your business logic.
Also make sure to register this route before your default route registration, in that case MVC will first try to match this route, and only if it doesn't match it will try to match the default route.
It sounds like you have the routes in the wrong order. When using MVC routing, the first match always wins, so you must place the most specific routes first before general routes.
routes.MapRoute(
name: "MyControllerAJAX",
url: "MyController/MyAjaxCall/{someParameter}",
defaults: new { controller = "MyController", action = "MyAjaxCall" }
)
routes.MapRoute(
name: "MyController",
url: "MyController/{id}",
defaults: new { controller = "MyController", action = "Index" }
)
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);