I have a public website which accepts a pagename, this then defaults to a controller and action with the pagename as the unique identifier to render the correct view. eg. http://www.mydomain.com/homepage
I also have a admin area where all CRUD stuff takes place access via a prefix of admin. eg. http://www.mydomain.com/admin/controller/action
Everything has been fine until I changed something recently and now when I got to http://www.mydomain.com/homepage the links I have such as:
<ul id="menu">
<li><%= Html.ActionLink("Home", "Details", "WebPage", new { pageName = "homepage" }, null)%></li>
<li><%= Html.ActionLink("About", "Details", "WebPage", new { pageName = "homepage" }, null)%></li>
</ul>
no longer appear as http://www.mydomain.com/homepage but instead http://www.mydomain.com/Admin/WebPage/Details?pageName=homepage
Can someone help?
Here is my Global.asax:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("AdminRoot",
"Admin",
new { controller = "Admin", action = "Index" }
);
routes.MapRoute(
"LogOn", // Route name
"LogOn", // URL with parameters
new { controller = "Account", action = "LogOn" },
new { action = "LogOn" }
);
routes.MapRoute("Account",
"Account/{action}",
new { controller = "Account", action = "" }
);
//routes.MapRoute(
// "Default", // Route name
// "{controller}/{action}/{id}", // URL with parameters
// new { controller = "Home", action = "Index", id = "" } // Parameter defaults
//);
routes.MapRoute(
"ErrorRoute", // Route name
"Error/Error404", // URL with parameters
new { controller = "Error", action = "Error404" }
);
routes.MapRoute("Admin",
"Admin/{controller}/{action}/{id}",
new { controller = "Admin", action = "Index", id = "" }
/*,new { action = "Create|Edit|Delete" }*/
);
routes.MapRoute("EventNewsData",
"Admin/{controller}/{action}/{year}/{month}",
new { controller = "Admin", action = "Index", year = 0, month = 0 }
/*,new { action = "Create|Edit|Delete" }*/
);
routes.MapRoute(
"Default", // Route name
"{pageName}/{moreInfoID}", // URL with parameters
new { controller = "WebPage", action = "Details", pageName = "homepage", moreInfoID = 0 },
new { action = "Details" }
);
routes.MapRoute("Error", "{*url}", new { controller = "Error", action = "Error404" });
}
UPDATE: This has fixed it but not really sure why
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("AdminRoot",
"Admin",
new { controller = "Admin", action = "Index" },
new { action = "Index" }
);
routes.MapRoute(
"LogOn", // Route name
"LogOn", // URL with parameters
new { controller = "Account", action = "LogOn" },
new { action = "LogOn" }
);
routes.MapRoute("Account",
"Account/{action}",
new { controller = "Account", action = "" }
);
//routes.MapRoute(
// "Default", // Route name
// "{controller}/{action}/{id}", // URL with parameters
// new { controller = "Home", action = "Index", id = "" } // Parameter defaults
//);
routes.MapRoute("Admin",
"Admin/{controller}/{action}/{id}",
new { controller = "Admin", action = "Index", id = "" }
, new { action = "Create|Edit|Delete|Index|DeleteFromIndex" }
);
routes.MapRoute("EventNewsData",
"Admin/{controller}/{action}/{year}/{month}",
new { controller = "Admin", action = "Index", year = 0, month = 0 }
, new { action = "GetCalendarData" }
);
routes.MapRoute(
"Default", // Route name
"{pageName}/{moreInfoID}", // URL with parameters
new { controller = "WebPage", action = "Details", pageName = "homepage", moreInfoID = 0 },
new { action = "Details" }
);
routes.MapRoute(
"ErrorRoute", // Route name
"Error/Error404", // URL with parameters
new { controller = "Error", action = "Error404" }
);
routes.MapRoute("Error", "{*url}", new { controller = "Error", action = "Error404" });
}
Maybe the problem is in action = "Details" constraint (There is no "{action}" in "{pageName}/{moreInfoID}"):
routes.MapRoute(
"Default", // Route name
"{pageName}/{moreInfoID}", // URL with parameters
new { controller = "WebPage", action = "Details", pageName = "homepage", moreInfoID = 0 },
new { action = "Details" }
);
UPDATED:
Now your code is using this route:
routes.MapRoute("Admin",
"Admin/{controller}/{action}/{id}",
new { controller = "Admin", action = "Index", id = "" }
/*,new { action = "Create|Edit|Delete" }*/
);
But you can use Html.RouteLink instead:
<ul id="menu">
<li><%= Html.RouteLink("Home", "Default", new { pageName = "homepage" })%> </li>
<li><%= Html.RouteLink("About", "Default", new { pageName = "homepage" })%> </li>
</ul>
UPDATED:
ASP.NET Routing looks for route with "Details" action and "WebPage" controller ("pageName" is optional) and match "Admin" route.
UPDATED:
Or add this route before "Admin" route:
routes.MapRoute("TheRoute",
"{pageName}/{moreInfoID}",
new { controller = "WebPage", action = "Details", moreInfoID = 0 },
new { pageName = "homepage" }
);
Related
I have next specific map routes
routes.MapRoute(
"MyPagePost",
"URL-Up/{name}",
new { controller = "MyController", action = "MyPostAction" },
new { httpMethod = new HttpMethodConstraint("POST") }
);
routes.MapRoute(
"MyPageGet",
"URL-Up/{name}",
new { controller = "MyController", action = "MyGetAction" },
new { name = "[A-Za-z].+", httpMethod = new HttpMethodConstraint("GET") }
);
my default controller looks like
routes.MapRoute(
name: "Default",
url: "{culture}/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", culture = "en", id = UrlParameter.Optional },
constraints: new { culture = #"[a-zA-Z]{2}" }
);
and the issue is next:
my get MyPageGet route show a page with include FORM with POST reqvest to MyPagePost route, but on a first call I am getting the same GET request and see in URL other extra param ?culture=de. Moreover, with or without this parameter, second call it working fine via MyPagePost route.
UPDATE:
In Chrome or fiddler Logs I see that reqvest to URL-Up/Bla-Bla has 302 status and response heared is URL-Up/Bla-Bla?culture=de. Why it can't be processed ?
just try it with
#using(Html.BeginRouteForm("MyPagePost",FormMethod.Post))
{
<input type="submit" value="Submit"/>
}
The routes in your post working for me in both html.beginform and html.beginrouteform on the first time.
i try it with the following routes and action methods
routes.MapRoute(
"MyPagePost",
"URL-Up/{name}",
new { controller = "Home", action = "PostAction" },
new { name="[A-Za-z].+", httpMethod = new HttpMethodConstraint("POST") }
);
routes.MapRoute(
"MyPageGet",
"URL-Up/{name}",
new { controller = "Home", action = "GetAction" },
new { name = "[A-Za-z].+", httpMethod = new HttpMethodConstraint("GET") }
);
routes.MapRoute(
name: "Default",
url: "{culture}/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", culture = "en", id = UrlParameter.Optional },
constraints: new { culture = #"[a-zA-Z]{2}" }
);
public ActionResult GetAction()
{
return View();
}
[HttpPost]
public ActionResult PostAction()
{
return View();
}
So I have the url:
www.mywebsite/signup/index
That works fine.
What I want to do is add a new route mapping to the RouteConfig class that allows the following url to work:
www.mywebsite/signup
I have tried:
routes.MapRoute(
name: "Signup",
url: "{controller}/",
defaults: new { controller = "Signup", action = "Index", id = UrlParameter.Optional }
And:
routes.MapRoute(
name: "Signup",
url: "{controller}",
defaults: new { controller = "Signup", action = "Index", id = UrlParameter.Optional }
And:
routes.MapRoute(
name: "Signup",
url: "{controller}/*",
defaults: new { controller = "Signup", action = "Index", id = UrlParameter.Optional }
My RouteConfig class has been customized and the default routing when creating a new ASP.NET MVC Project has been changed, it currently looks like this:
routes.MapRoute(
name: "DefaultConstrained",
url: "{controller}/{action}/{id}",
defaults: new { action = "Index", controller = "Home", id = UrlParameter.Optional },
constraints: new { controller = ConstrollersAsCSV() }
);
routes.MapRoute(
name: "SiteLogin",
url: "{site}",
defaults: new { controller = "User", action = "Login", site = "" },
constraints: new { site = new ExcludeAndUseRegex() }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "User", action = "Login", id = UrlParameter.Optional }
);
Where the constraint method ConstrollersAsCSV (which I don't know what that is doing) is:
public static string ConstrollersAsCSV(string optional = null)
{
var list = GetControllerNames();
var sb = new StringBuilder();
sb.Append("(");
if (!string.IsNullOrWhiteSpace(optional))
sb.Append(optional);
foreach (var cont in list)
{
if (sb.Length > 1)
sb.Append("|");
sb.Append(cont.Replace("Controller", ""));
}
sb.Append(")");
return sb.ToString();
}
And the implementation of GetControllerNames is:
public static List<string> GetControllerNames()
{
List<string> controllerNames = new List<string>();
GetSubClasses<Controller>().ForEach(
type => controllerNames.Add(type.Name.ToLower()));
GetSubClasses<ApiController>().ForEach(
type => controllerNames.Add(type.Name.ToLower()));
controllerNames.Add("elmah");
return controllerNames;
}
And the implementation of GetSubClasses is:
private static List<Type> GetSubClasses<T>()
{
return Assembly.GetCallingAssembly().GetTypes().Where(
type => type.IsSubclassOf(typeof(T))).ToList();
}
The error I am getting when going to www.mywebsite.com/signup is:
**HTTP Error 403.14 - Forbidden**
I have created one custom route
routes.MapRoute("Exams",
"Exams/{id}/{name}",
new { controller = "Exam", action = "SingleExam", id = "", name = (string) null },
new[] { "MockTests.FrontEnd.Controllers" }
);
For this I am creating link as
http://localhost:61575/Exams/12/maharashtra+mba+common+entrance+test
But his gives error as
The resource cannot be found
SingleExam code
public ActionResult SingleExam(int id,string name)
{
return View();
}
Other Routes
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "User", action = "Register", id = UrlParameter.Optional },
new[] { "MockTests.FrontEnd.Controllers" }
);
What wrong I am doing ?
How do I make my application route to mydomainname/username/controller.
I am working on asp.net mvc web application that enables a user to belong to multiple account. ie. In the application every account has its own users, and each user in one account can also be a user in another account. What i need is when a user wants to login, they specify the account they want to be logged to like this: domainname.com/accountname/login.
Am able to do this, but where am having issue is how to persist the accountname route parameter across other routes? I mean making it to be visible on the url. For now am using cookie to store and get the accountname parameter, but i need a way to make it visible on the url in every request (without having to manually route it on links) until the user singout.
Am using asp.net mvc 2
Edit: Added my route code
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("", "", new { controller = "Home", action = "index" });
routes.MapRoute("", "dashboard", new { controller = "account", action = "dashboard" });
routes.MapRoute("", "contacts", new { controller = "contact", action = "index" });
routes.MapRoute("", "groups", new { controller = "group", action = "index" });
routes.MapRoute("", "sms", new { controller = "sms", action = "index" });
routes.MapRoute("", "users", new { controller = "user", action = "index" });
routes.MapRoute("", "login", new { controller = "Home", action = "login", accountUrlName = UrlParameter.Optional });
routes.MapRoute("", "{accountUrlName}/login", new { controller = "Home", action = "login" });
routes.MapRoute("", "register", new { controller = "home", action = "register" });
routes.MapRoute("", "{accountUrlName}/invitations/{ivkey}", new { controller = "home", action = "invitations" });
routes.MapRoute("", "{urlName}",
new { controller = "home", action = "index", urlName = UrlParameter.Optional });
routes.MapRoute("", "{accountUrlName}/{action}",
new { controller = "account", action = "dashboard", id = "", accountUrlName = UrlParameter.Optional });
routes.MapRoute("", "{accountUrlName}/{controller}/{action}/{id}",
new { controller = "account", action = "dashboard", id = "", accountUrlName = ""});
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
So you effectively have two sets of routes here -
{AccountName}/{Controller}/{Action} and {Username}/{Controller}/{Action}.
Is this right?
It's possible for you to create these routes, but you'd have to have usernames which do not contain account names, and vice versa.
I could have SWORN I had this working, but somewhere along the line I apparently broke it. Maybe it was during my migration from ASP.NET MVC in VS2008 to ASP.NET MVC2 in VS2010.
My ActionLink:
Html.ActionLink(segment.TitleWithChapterNumber, "Index", "Read", new { bookCode = Model.Product.Id, segmentCode = segment.Index }, null)
The route I expect it to match:
routes.MapRoute(
"Read",
"Read/{bookCode}/{segmentCode}/{sectionCode}/{renderStrategy}",
new { controller = "Read", action = "Index", bookCode = "", segmentCode = "", sectionCode = "", renderStrategy = "" }
);
This renders a link that looks like: http://localhost/Obr/Read?bookCode=14&segmentCode=0
But I want it to look like http://localhost/Obr/Read/14/0
Clicking the link that it renders does take me to the right controller and the response is accurate. If I paste in the link I WANT it to look like, it does work. I guess it's just not matching?
Am I missing something obvious? I've stared at it so long I don't even know what I am looking for anymore.
For reference, here are ALL of my routes:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"ReadImage",
"Read/Image/{bookId}/{imageName}",
new { controller = "Read", action = "Image" }
);
routes.MapRoute(
"Read",
"Read/{bookCode}/{segmentCode}/{sectionCode}/{renderStrategy}",
new { controller = "Read", action = "Index", bookCode = "", segmentCode = "", sectionCode = "", renderStrategy = "" }
);
routes.MapRoute(
"BookReport",
"BookReport/{action}/{folder}",
new { controller = "BookReport", action = "Details", folder = "" }
);
routes.MapRoute(
"Reference",
"Reference/Details/{referenceType}/{searchText}",
new { controller = "Reference", action = "Details", referenceType = "", searchText = "" }
);
routes.MapRoute(
"PaginatedAudits", // Route name
"Audit/Page/{pageNumber}", // URL with parameters
new { controller = "Audit", action = "Index" } // Parameter defaults
);
routes.MapRoute(
"PaginatedReadLog", // Route name
"ReadLog/Page/{pageNumber}", // URL with parameters
new { controller = "ReadLog", action = "Index" } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
The action signature looks like this:
[Authorize]
public ActionResult Index(string bookCode, string segmentCode, string sectionCode, string renderStrategy)
{
// code
}
Try a route link by explicitly giving the name of the route:
Html.RouteLink(
segment.TitleWithChapterNumber, // linkText
"Read", // routeName
new {
bookCode = Model.Product.Id,
segmentCode = segment.Index
}, // routeValues
null // htmlAttributes
)
Your route definitions should have UrlParameter.Optional instead of empty strings.
routes.MapRoute(
"Read",
"Read/{bookCode}/{segmentCode}/{sectionCode}/{renderStrategy}",
new { controller = "Read", action = "Index", bookCode = UrlParameter.Optional, segmentCode = UrlParameter.Optional, sectionCode = UrlParameter.Optional, renderStrategy = UrlParameter.Optional }
);
This will also help with redirect in controllers and form urls created through MVC extensions.