Invalid url in form rendered by html.beginform - asp.net

I have an application which is mix of WebForms and MVC.
HomeController is a simple controller:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
On view - /Views/Home/Index.cshtml, I have used Html.BeginForm to render the form.
#using(Html.BeginForm("Index", "Home", FormMethod.Post, new { #class = "form-horizontal"}))
{
#Html.TextBox("username", "");
#Html.Password("password", "");
<button type="submit">Login</button>
}
Routes are configured like below. First 2 routes are using MapPageRoute to map to webforms.
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapPageRoute("admin", "admin", "~/admin.aspx");
routes.MapPageRoute("adminEdit", "adminEdit", "~/adminEdit.aspx");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
But after my homepage ("Home" is my default controller) is rendered to the browser, form action has url:
/admin?action=Index&controller=Home
not "/Home/" even though i have passed "Index" & "Home" as actionname and controllername to Html.Beginform respectively.
Url routing is working fine when i browse to my homepage. But the form's "action" has invalid url. It seems like it is matching the first route i have configured. Why?

I got it to work by following the instructions in this post:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapPageRoute(
routeName: "admin",
routeUrl: "{pagename}",
physicalFile: "~/{pagename}.aspx");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
However, I cannot explain why the parameter is necessary to make it not match in this case.

Related

How to redirect page from default route to custom one in MVC application?

I would like to know whether there is a way to redirect page from "~/ControllerName/Index" to custom route like "~/example-custom" without using 301 redirect in URL rewrite module.
I have used routing attribute:
[Route("example-custom")]
public ActionResult Index()
{
return View();
}
But with this routing attribute "~/ControllerName/Index" is no more. When users go to "~/ControllerName/Index", page does not exist on that route. I would like redirect users to "~/example-custom" when they want to access page.
So, what I would like in shorter terms is to browser display link "something.com/example-custom" and not "something.com/ControllerName/Index". But also accessing "something.com/ControllerName/Index" would redirect automatically to "something.com/example-custom".
Add custom route into RouteConfig.cs File
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "example-custom",
url: "{example-custom}/{id}",
defaults: new { controller = "YourControllerName", action = "YourActionName", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}

How to use short urls for categories in MVC

Short urls containing product categories like
http://example.com/Computers
Should used in ASP.NET MVC 4 shopping cart.
If there is no controller, Home Index method with id parameter as Computers should called.
I tried to add id parameter to home controller using
public class HomeController : MyControllerBase
{
public ActionResult Index(string id)
{
if (!string.IsNullOrWhiteSpace(id))
{
return RedirectToAction("Browse", "Store", new
{
id = id,
});
}
return View("Index", new HomeIndexViewModel());
}
but http://example.com/Computers causes 404 error
Server Error in '/' Application.
The resource cannot be found.
Description: 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.
Requested URL: /Computers
Version Information: Microsoft .NET Framework Version:4.0.30319;
ASP.NET Version:4.6.1073.0
How to force some controller call if there is no controller defined after slash in http://example.com/....
MVC default routing is used:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{*favicon}", new { favicon = #"(.*/)?favicon.ico(/.*)?" });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
It looks like MVC ignores routing parameter:
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
Howe to fix this ?
your problem is because aspnet mvc is trying to find a controller with the name Computers, and your controller is Home, you can add a new route like this before the route with name Default:
routes.MapRoute(
name: "Computers",
url: "Computers",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
In the above case, you are creating a route with match with the url http://domain.com/Computers and this route will be manage by the HomeController.
Also, according to your comment, you can have a route like:
routes.MapRoute(
name: "Default",
url: "{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

asp.net mvc 4 controller routing depending on url

Depending on url, I want to route different controllers. So depending on different URLs directed to same DNS server, I want to give my website a different look and feel.
To test this locally I tried:
public class RouteConfig
{
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(
"localhost",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Contact", id = UrlParameter.Optional }
);
}
}
Wat I wanted was the Contact page to appear, not the default Home Page as the URL was :/localhost:portnr./.
How can I get the first bit of an URL (domain) to decide which controller to route?
Suppose you have the Contact controller and Index Action, and you want to load that on page load,
you can specify as
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Contact", action = "Index", id = UrlParameter.Optional }
);

ASP.NET MVC 2 route not resolving properly (or, rather, how I think it should)

I've never come across the need to attempt the following such routes, where a user can visit another's home page, or their own (admin) home page. Here are the routes I have:
routes.MapRoute(null, "Home/Me",
new { controller = "Home", action = "Admin" });
routes.MapRoute(null, "Home/{userID}",
new { controller = "Home", action = "Visitor" });
Apparently I've incorrectly assumed that "Home/6e982cc5-4d1d-4232-947b-835e54e49c7" will resolve to the following action on the Home controller:
public ActionResult Visitor(Guid userID) {}
Would anyone be kind enough to explain why this doesn't work like I think it should?
Assuming the following routes setup:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
null,
"Home/Me",
new { controller = "Home", action = "Admin" }
);
routes.MapRoute(
null,
"Home/{userID}",
new { controller = "Home", action = "Visitor" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Home/4 will resolve to the Visitor action of the Home controller but the default model binder will throw an exception because 4 is not a valid value for a Guid.
On the other hand Home/6e982cc5-4d1d-4232-947b-835e54e49c7 should work. Home/Me will resolve to the Admin action on the Home controller.

Dynamic routes and Membership providers

Using the a Membership provider and the MVC framework, is it possible that routes are dynamically changed so that a already logged in user goes to his own page, rather than the default.
At the moment I go to the default. If the user is already logged in or not, there I do a redirect to their own page. This can't be the correct way! Can it?
In RegisterRoutes I have this
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
One option would be to use a Route Constraint.
public class AuthenticatedConstraint : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
return httpContext.Request.IsAuthenticated;
}
}
Then you could define a "LoggedInDefault" route before the regular default:
routes.MapRoute(
"LoggedInDefault",
"{controller}/{action}/{id}",
new { controller = "LoggedIn", action = "Index", id = "" },
new { loggedIn= new AuthenticatedConstraint()}
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);

Resources