What my requirement is;
if I type www.mysite.com - it will load the front page.
But if I type www.mysite.com/john - it will return john's profile page.
John's profile is originally located in /Profile/John and this works fine. But the requirement is www.mysite.dom/John.
I am trying many ways with NO success. Would be nice if anyone can help me out.
cheers
Define two routes.
First to "/Profile/{name}", then to "/{name}", pointing to the same action.
If you want to use the same controller and have routes setup to respond to parameters, you can setup your routes like this too
routes.MapRoute(
name: "Meeting",
url: "{name}",
defaults: new
{
controller = "Home",
action = "Welcome"
}
);
routes.MapRoute(
name: "Default",
url:"",
defaults: new
{
controller = "Home",
action = "Index"
}
);
My HomeController looks like
public void Welcome(string name)
{
ViewBag.Title = "Home Page";
}
public ActionResult Index()
{
return View();
}
Related
I use VS2013 and created MVC application by wizard. I also deleted all extra files and have the following:
1) RouteConfig.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
}
2) HomeController.cs
public class HomeController : Controller
{
[Route("Home/Index")]
public ActionResult Index()
{
return View();
}
}
3) Index.cshtml
#{
ViewBag.Title = "Home Page";
}
Home page
I've got the page with error:
HTTP 403.14 - Forbidden
But, if I add to URL in browser's address bar manually - Home/Index:
http://localhost:50600/Home/Index
The page appears.
What I'm doing wrong?
Remove the "Home" from the route as the controller name HomeController is already starting your route with "Home". If you want to change that "Home" prefix, you can add an attribute to the HomeController class to define that.
Also, the default route name for an action will match the action name, so in this case you could use [Route("")] and the url /Home/Index would work.
My guess is that when you try this url:
http://localhost:50600
It does not work because you have removed the default route from your routes config. I don't know if you removed it yourself, but RoutesConfig.cs file usually comes with the following default route:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
This code ensures that if the user does not provide the controller or the action the site will default to index action of the home controller (you ca see that under the defaults parameter). This would also explain why it works when you try this route:
http://localhost:50600/Home/Index.
I think I know what's your problem now. You're expecting the default url to show up your Index View in HomeController but you've not setup the default route. You can set the default route by adding the following lines in your RouteConfig.cs
config.Routes.MapRoute(
name: "Default",
routeTemplate: "{controller}/{action}",
defaults: new { controller = "Home", action = "Index" }
);
Alternatively, if you wish to use attribute routing only without mixing with route template, you can just add the default route as following:-
config.Routes.MapRoute(
name: "Index",
url: "",
defaults : new { controller = "Home", action = "Index" }
);
I'm building an intranet where I have the following home controller:
[Route("{action=index}")]
public class HomeController : Controller
{
public ActionResult Index()
{
return View(HomeModelBuilder.BuildHomeModel());
}
public ActionResult FormsHome()
{
return View(HomeModelBuilder.BuildFormsHomeModel());
}
}
I'm trying to get my forms homepage to have a url of http://intranet/forms so I thought I could do this using the following routing attribute:
[Route("~/forms")] // have also tried 'forms' and '/forms'
public ActionResult FormsHome()
but when I go to the url, it complains that multiple controllers have that route:
The request has found the following matching controller types:
HRWebForms.Website.Controllers.ChangeDetailsController
HRWebForms.Website.Controllers.InternalTransferController
HRWebForms.Website.Controllers.LeaverController
...
I have also tried adding [RoutePrefix("")] to the controller but this didn't work either
Is there a way to give that action a url of "forms" (without any controller or without adding a separate forms controller with an index) by just using routing attributes?
You could try adding [RoutePrefix("forms")] to your controller, but this will result in all your actions expecting the same prefix.
There is a walkaround for this too (by using [Route("~/RouteParam/AnotherRouteParam")] to have Route "RouteParam/AnotherRouteParam") but it seems to me that FormsController would cost less work.
Ok so ranquild's comment pushed me in the right direction. In my route config, I had the default route of
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
So that my homepage would still work on the url with nothing in. If I changed this to
// Needed for homepage
routes.MapRoute(
name: "Home",
url: "",
defaults: new { controller = "Home", action = "Index" }
);
// Needed for Html.ActionLink to work
routes.MapRoute(
name: "Default",
url: "{controller}/{action}",
defaults: new { controller = UrlParameter.Optional, action = UrlParameter.Optional }
);
It seemed to solve the problem
I have the following code for the navigation bar at the top of my application:
<li>#Html.ActionLink("Users", "Index", "Account")</li>
<li>#Html.ActionLink("Models", "Index", "Models")</li>
In AccountController I have the following Index function:
[Authorize(Roles = "Admin, CanEditGroup, CanEditUser")]
public ActionResult Index()
{
var users = _db.Users;
var model = new List<EditUserViewModel>();
foreach (var user in users)
{
var u = new EditUserViewModel(user);
model.Add(u);
}
return View(model);
}
In ModelsController I have the following Index function:
public ActionResult Index()
{
return View();
}
When I click on the 'Users' link I am taken to '/Account' and the Index is correctly shown. However, when I click the 'Models' link I am taken to '/Models' and receive a 'HTTP Error 403.14 - Forbidden' error. When I debug this I can see that the Index function in 'ModelsController' isn't being hit. '/Models/Index' works fine though.
It should be noted that I am using a template where AccountController already existed. I have since added ModelsController.
Finally, here is my simple routing file:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Please help me understand the difference between AccountController and ModelsController which is causing a difference in routing behaviour.
i think you already have a models folder or some other resource with named models in your project.
you can change your controller name.
or set RouteExistingFiles for your route collections
routes.RouteExistingFiles = true;
will help
In ASP.NET MVC 4 I wonder about the behavior, how links are generated for me.
Imagine a simple controller with 3 actions, each taking an integer parameter "requestId", for example:
public class HomeController : Controller
{
public ActionResult Index(int requestId)
{
return View();
}
public ActionResult About(int requestId)
{
return View();
}
public ActionResult Contact(int requestId)
{
return View();
}
}
and this registered route (before the default route):
routes.MapRoute(
name: "Testroute",
url: "home/{action}/{requestId}",
defaults: new { controller = "Home", action = "Index" }
);
I call my index-view using http://localhost:123/home/index/8
On this view, I render links for the other two actions:
#Html.ActionLink("LinkText1", "About")
#Html.ActionLink("LinkText2", "Contact")
Now I expect MVC to render this links including the current route-value for "requestId", like this:
http://localhost:123/home/about/8
http://localhost:123/home/contact/8
But i get these links (without the paramter):
http://localhost:123/home/about
http://localhost:123/home/contact
...but not for the index-action if i would specify one:
#Html.ActionLink("LinkText3", "Index")
What I want to avoid is to explicitly specify the parameters in this manner:
#Html.ActionLink("LinkText1", "Contact", new { requestId = ViewContext.RouteData.Values["requestId"] })
When I move the requestId parameter before the action paramter it works like I expect it, but I don't want to move it:
routes.MapRoute(
name: "Testroute",
url: "home/{requestId}/{action}",
defaults: new { controller = "Home", action = "Index" }
);
Can someone explain me this behavior? How can I get this to work without specifying the parameter explicitly?
InController:
Replace the int to nullable int
For Routing:
set requestId as optional in routing
routes.MapRoute(
name: "Testroute",
url: "home/{action}/{requestId}",
defaults: new { controller = "Home", action = "Index" ,requestId=RouteParameter.Optional }
);
I currently have the following route set up:
context.MapRoute("Root", "", new
{
controller = "Server",
action = "FavoritesList",
id = "00C"
}
);
However I would like to change this so the default goes to the following:
/F00C/Home-About#/C01C/Overview
I realize this doesn't map to controllers and actions but is there a way I can just do an internal redirect with the MapRoute to a another href.
If you mean you would like your default page to be that meaning that if someone hits your root / you would like them to be redirected to /F00C/Home-About#/C01C/Overview, then simply assuming you have these routes in global.asax.cs
routes.MapRoute(
"DefaultRedirect", // Route name
string.Empty, // URL with parameters
new { controller = "Home", action = "Redirect" });
routes.MapRoute(
"Homepage",
"F00C/Home-About",
new { controller = "Home", action = "Index" });
You can do this in your HomeController:
public ActionResult Redirect()
{
return Redirect("~/F00C/Home-About#/C01C/Overview");
}
public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
EDIT: Forgot to say
You can also just configure a redirect in IIS itself if that is more to your liking, but this way it is part of the application.