Asp.net Mvc Routing issue - asp.net

I have controller named PartialController and have a action method named AboutMe.
I can access my action method from localhost/Partial/AboutMe . But i want to access it from localhost/About. So i changed my route like this:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Blog", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "About",
url: "About",
defaults: new
{
controller = "Partial",
action = "AboutMe",
id = UrlParameter.Optional
}
);
But i got 404 exception when trying to access AboutMe action from localhost/About. Need advice.

Define your About Route first. Order matters when it comes to routing, they are applied in the order in which they are defined.
routes.MapRoute(
name: "About",
url: "About",
defaults: new
{
controller = "Partial",
action = "AboutMe",
id = UrlParameter.Optional
}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Blog", action = "Index", id = UrlParameter.Optional }
);

Related

Routing deletes "/Index" from URL

in my application I have a CompanyGroupDataController.cs.
When the execution gets into its main action (Index), through this URL:.../CompanyGroupData/Index, the URL not longer ends in "/Index", so it erases the last part (althoug in the browser bar it keeps it). I think the problem is in my RouteConfig.cs which is this:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{company}/{controller}/{action}/{id}",
defaults: new
{
company = Skill4IT.WebApps.Admin.Helpers.Tools.AdminUrlAccessCode,
controller = "Home",
action = "Index",
id = UrlParameter.Optional
},
namespaces: new string[] { "Skill4IT.WebApps.Admin.Controllers" }
);
routes.MapRoute(
name: "spa",
url: "#{controller}/{action}/{id}",
defaults: new
{
company = "admin",
controller = "Home",
action = "Index",
id = UrlParameter.Optional
},
namespaces: new string[] { "Skill4IT.WebApps.Admin.Controllers" }
);
routes.MapRoute(
name: "Catchall",
url: "{*catchall}",
defaults: new
{
controller = "Error",
action = "Error404",
}
);
Thanks

How to pass string as parameter in ASP.Net MVC

I am trying to make a simple sample of passing string parameter in URL not using the queryString at this example. First of all I added a new MapRouteto RouteConfig.cs file as
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "app",
url: "{controller}/{action}/{name}"
);
and in Controller I have
public class AppController : Controller
{
public string Index(string name)
{
return HttpUtility.HtmlEncode("Hello " + name );
}
}
but the view is not displaying the string parameter. For example a URL like this http://localhost:59013/App/Index/Ali only return Hello!
Why is this happening?
Update
routes.MapRoute(
name: "app",
url: "{controller}/{action}/{name}",
defaults: new {action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
First, you need to change the order of your registered routes so the default will be the last.
Secondly, your app route pattern is wrong and will always collide with the default route. You better change its pattern to
url: "App/Index/{name}"
Or perhaps to the more friendlier
url: "App/{name}"
Both with
defaults: new { controller = "App", action = "Index" }
So that your routes would look like:
routes.MapRoute(
name: "app",
url: "App/{name}",
defaults: new { controller = "App", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
See MSDN

multiple routes with same url

I currently have the following in my RouteConfig.cs:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Identity",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Profile", action = "Identity" }
);
I'm trying to use the following in a Action method:
return RedirectToRoute("Identity", new { #id = id });
but it doesn't seem to go there, when I check with Fiddler, i see the request is going back to the same page i was currently on, it appears to be hitting the Default route. Is there anyway to force it to get the Identity one, even if their urls are the same, I want to use that to force users to the other controller when needed.
MVC will stop looking for a route as soon as a match is found. In your case, it will look in the following order: 1.) Default 2.) Identity.
If you want to create a specific route with the same pattern, you can achieve it with the following code:
routes.MapRoute(
name: "Identity",
url: "Profile/{action}/{id}",
defaults: new { controller = "Profile", action = "Identity" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
You do not need to 'Force' the user to another controller. It is a good practice to use the RedirectToAction method so you can supply the controller and action you want to bring the user to.
The solution supplied by #thiag0 will not work.
Use the following
return RedirectToAction("Identity", "Profile", new { id = 5 });
and in your Profile controller, make sure you can accept the parameter
public ActionResult Identity(int id)
{
return View();
}
and in RouteConfig.cs
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Identity",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Profile", action = "Identity"}
);

Rename the Controller Name in the URL

I have 2 brands which use the same controller. In one brand the URL must look like ~Home/Index and in other URL should look like ~Account/Index, but the both URLs must point to the same Home Controller action methods.
Please give any idea how to achieve this.
Specify 2 route in the RouteConfig.cs
routes.MapRoute(
name: "Default",
url: "Home/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default1",
url: "Account/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Use this URL to call the Index() method in Home controller
1) http://localhost/../Home/Index
2) http://localhost/../Account/Index
Put below code inside Global.asax.cs
routes.MapRoute(
name: "Account",
url: "Account/Index",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "Home/Index",
defaults: new { controller = "Home", action = "Index" }
);
*Note:Always Default name put in last line in golbal.asax.cs

Configuring asp.net mvc routing

I'm using this code for routing in RouteConfig.cs :
routes.MapRoute("Default", "", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
Now i want to change it, if the url is "<domain>/Saman/Profile", i should load the profile page for the user that username is "Saman".
Actually you have to give profile route before the default route.here url will look for the profile once it's find it will use the first route.i hope this will work.
routes.MapRoute(
name: "Profile",
url: "FilesView/{name}",
defaults: new { controller = "FilesView", action = "Profile", name = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "FilesView", action = "Index", id = UrlParameter.Optional }
);

Resources