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
Locally the website works fine, when deployed to azure we get the view index or its master was not found or no view engine supports the searched locations.
Routeconfig
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "websitename",
url: "",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "{controller}", action = "{action}", id = UrlParameter.Optional }
);
We are using angularjs and having no problems until deployed to azure other than this issue.
You define following for home page right?
routes.MapRoute(
name: "DoransData",
url: "",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Change it to
routes.MapRoute(
name: "DoransData",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Based on your code I think that you could only need following code
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 }
);
}
i have a RouteConfig.cs file, and i don't know which will be excute, please tell me, and explain to me.
It's about asp.net MVC4
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
#region Special Page
// Admin Router
routes.MapRoute(
name: "Admin",
url: "Admin/",
defaults: new { area = "Admin", controller = "dashboard", action = "index" },
namespaces: new[] { "PixelCMS.Controllers" }
);
// Error Page, NotFound And UnderConstruction
routes.MapRoute(
name: "ErrorsLang",
url: "{culture}/Errors/{action}",
defaults: new { culture = CultureHelper.GetDefaultCulture(), controller = "Errors", action = "NotFound" },
constraints: new { culture = "[a-z]{2}" },
namespaces: new[] { "PixelCMS.Controllers" }
);
routes.MapRoute(
name: "Errors",
url: "Errors/{action}",
defaults: new { culture = CultureHelper.GetDefaultCulture(), controller = "Errors", action = "NotFound" },
namespaces: new[] { "PixelCMS.Controllers" }
);
// Home
//routes.MapRoute(
// name: "Home",
// url: "{culture}/",
// defaults: new { culture = CultureHelper.GetDefaultCulture(), controller = "Home", action = "Index", type = "", slug = "" },
// namespaces: new[] { "PixelCMS.Controllers" }
//);
// Common
routes.MapRoute(
name: "Common",
url: "Common/{action}/{id}",
defaults: new { controller = "Common", action = "", id = UrlParameter.Optional },
namespaces: new[] { "PixelCMS.Controllers" }
);
// Common
routes.MapRoute(
name: "Widget",
url: "Widget/{action}/{id}",
defaults: new { controller = "Widget", action = "", id = UrlParameter.Optional },
namespaces: new[] { "PixelCMS.Controllers" }
);
// Login
routes.MapRoute(
name: "LoginPanel",
url: "account/{action}",
defaults: new { controller = "Account", action = "Register" },
namespaces: new[] { "PixelCMS.Controllers" }
);
// Login
routes.MapRoute(
name: "Account",
url: "account/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional },
namespaces: new[] { "PixelCMS.Controllers" }
);
routes.MapRoute(
name: "AccountLang",
url: "{culture}/account/{action}/{id}",
defaults: new { culture = CultureHelper.GetDefaultCulture(), controller = "Account", action = "Login", id = UrlParameter.Optional },
constraints: new { culture = "[a-z]{2}" },
namespaces: new[] { "PixelCMS.Controllers" }
);
// Contact
routes.MapRoute(
name: "ContactLang",
url: "{culture}/Contact/{add}",
defaults: new { culture = CultureHelper.GetDefaultCulture(), controller = "Content", action = "Contact", add = UrlParameter.Optional },
constraints: new { culture = "[a-z]{2}" },
namespaces: new[] { "PixelCMS.Controllers" }
);
routes.MapRoute(
name: "Contact",
url: "Contact/{add}",
defaults: new { culture = CultureHelper.GetDefaultCulture(), controller = "Content", action = "Contact",add=UrlParameter.Optional },
namespaces: new[] { "PixelCMS.Controllers" }
);
// Search
routes.MapRoute(
name: "SearchLang",
url: "{culture}/Search/",
defaults: new { culture = CultureHelper.GetDefaultCulture(), controller = "Content", action = "Search" },
constraints: new { culture = "[a-z]{2}" },
namespaces: new[] { "PixelCMS.Controllers" }
);
routes.MapRoute(
name: "Search",
url: "Search/",
defaults: new { culture = CultureHelper.GetDefaultCulture(), controller = "Content", action = "Search" },
namespaces: new[] { "PixelCMS.Controllers" }
);
// cart
routes.MapRoute(
name: "ShoppingCartLang",
url: "{culture}/ShoppingCart/",
defaults: new { culture = CultureHelper.GetDefaultCulture(), controller = "ShoppingCart", action = "Index", type = "", slug = "" },
constraints: new { culture = "[a-z]{2}" },
namespaces: new[] { "PixelCMS.Controllers" }
);
routes.MapRoute(
name: "ShoppingCart",
url: "ShoppingCart/",
defaults: new { culture = CultureHelper.GetDefaultCulture(), controller = "ShoppingCart", action = "Index", type = "", slug = "" },
namespaces: new[] { "PixelCMS.Controllers" }
);
#endregion
// Slug
//routes.MapRoute(
// name: "Slug",
// url: "{culture}/{slug1}/{slug2}/{slug3}/{slug4}",
// defaults: new { culture = UrlParameter.Optional, controller = "Content", action = "Slug", slug1 = UrlParameter.Optional, slug2 = UrlParameter.Optional, slug3 = UrlParameter.Optional, slug4 = UrlParameter.Optional },
// namespaces: new[] { "PixelCMS.Controllers" }
//);
routes.MapRoute(
name: "SlugLang",
url: "{culture}/{slug1}/{slug2}/{slug3}/{slug4}",
defaults: new { culture = CultureHelper.GetDefaultCulture(), controller = "Content", action = "Slug", slug1 = UrlParameter.Optional, slug2 = UrlParameter.Optional, slug3 = UrlParameter.Optional, slug4 = UrlParameter.Optional },
constraints: new { culture = "[a-z]{2}" },
namespaces: new[] { "PixelCMS.Controllers" }
);
routes.MapRoute(
name: "Slug",
url: "{slug1}/{slug2}/{slug3}/{slug4}",
defaults: new { culture = CultureHelper.GetDefaultCulture(), controller = "Content", action = "Slug", slug1 = UrlParameter.Optional, slug2 = UrlParameter.Optional, slug3 = UrlParameter.Optional, slug4 = UrlParameter.Optional },
namespaces: new[] { "PixelCMS.Controllers" }
);
`
And i wonder that will RouteConfig file all the links of site?
i am a newbie about MVC4, and route file and
#HTML.Action("LoadURL","Common",new {...})
#Html.Action().
command is correspond of Routeconfig.cs
I am sorry writing you back in reply instead of writing in comment. ( don't have enough reputation to write back in comment)
Routes work in the order they are defined, in your case you have defined multiple routes and when ever you make any request it will start from top and traverse to the bottom, first one matched with the rule will get hit.
And in most of the cases you don't need these many route sections, only few logical one can help you. For an instance
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
This will try to match url with controller name and then see if that controller has any action and in this case id is optional.
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 }
);
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 }
);