How to create a route for www.website.com/profile - c#

I'm coding a website like a platform where we can access the user profile from the following URL:
www.mywebsite.com/DanielVC
The controller that has the details about the profile is the following
Controller: Perfil
Action: Perfil
I already have the following Route for all application:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Pages", action = "Index", id = UrlParameter.Optional }
);
I already tried to create the following route:
routes.MapRoute(
name: "Perfil",
url: "Pages/{id}",
defaults: new { controller = "Pages", action = "Index", id = UrlParameter.Optional }
);
But didn't work

Put this BEFORE (above) the default route.
routes.MapRoute(
name: "Perfil",
url: "{id}",
defaults: new { controller = "Pages", action = "Index", id = UrlParameter.Optional }
);
Also, this will result in every route to be redirected to Perfil route. You must create a redirection in that action if a username is not found (e.g. mywebsite.com/randomuserthatdoesntexist) and/or other routes (mywebsite.com/contact).
EDIT
Example for your method
public class PagesController : Controller
{
public ActionResult Index(string id)
{
if (matchesOtherRoute(id))
RedirectToAction("OtherAction", "OtherController");
if (!userExists(id))
RedirectToAction("NotFoundAction", "ErrorController");
// Do other stuff here
}
}

it should be like
routes.MapRoute(
name: "Perfil",
url: "Pages/{id}",
defaults: new { controller = "Perfil", action = "Perfil", id = UrlParameter.Optional }
);

Related

Custom URL in mvc .net

I currently have the following url implemented:
https://example.com/controller/challenge/{params}
and would like to create a second url that accepts a different set of parameters: https://example.com/controller/v2/challenge/{params}.
I cannot seem to get the "v2" to be hardcoded into the url path. Rather, the only way I can make it work at the moment is using https://example.com/controller/challengev2/{params}
In my configuration file:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "controller", action = "Challenge", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "ChallengeV2",
url: "{controller}/{action}/{id}",
defaults: new { controller = "controller", action = "Challengev2", id = UrlParameter.Optional }
);
My controller is set up like:
public async Task<ActionResult> Challenge(string resumePath, string refid, string client_id)
{
}
[ActionName("Challengev2)]
public async Task<ActionResult> Challenge(string refid)
{
}
I have tried modifying the url when defining the route to:
routes.MapRoute(
name: "ChallengeV2",
url: "controller/v2/Challenge/{id}",
defaults: new { controller = "controller", action = "Challengev2", id = UrlParameter.Optional }
);
But this seems to throw a 404 error. Is there a step that I am missing to create that endpoint?
Switch the order you define your routes. The order plays an extremely important role.
routes.MapRoute(
name: "ChallengeV2",
url: "controller/v2/Challenge/{id}",
defaults: new { controller = "controller", action = "Challengev2" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "controller", action = "Challenge", id = UrlParameter.Optional }
);
To create a v2 route prefix, I would recommend you to use route attribute instead of route map.
Using route attribute, your code would be like this:
public class ChallengeController
{
[Route("/challenge/{resumePath}/{refid}/{client_id}")]
public async Task<ActionResult> Challenge(string resumePath, string refid, string client_id)
{
// ...
}
[Route("/v2/challenge/{refid}")]
public async Task<ActionResult> Challenge(string refid)
{
// ...
}
}

How to use multiple route in mvc5

Check the RouteConfig.cs file code bellow on mvc5 project. The first Default route which is configured for return home page is works fine. But the second one which i made to send traffic to Product controller is not working. The way i am trying to hit the controller is- http://localhost:50070/Product/somepage/good-product
The error i am getting is:
The resource cannot be found.
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(
name: "Product",
url: "Product/{pagename}/{slug}",
defaults: new { controller = "Product", action = "Index", slug = UrlParameter.Optional },
namespaces: new[] { "Demo.Controllers" }
);
}
}
You problem is in order of route mappings. Routes are matched in mapping order - in your case Product/somepage/good-product matches default route template and it is chosen. But you don't have action Somepage on Product controller.
Default route mapping should be the last mapping (just change the order):
routes.MapRoute(
name: "Product",
url: "Product/{pagename}/{slug}",
defaults: new { controller = "Product", action = "Index", slug = UrlParameter.Optional },
namespaces: new[] { "Demo.Controllers" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Please change second routes like that
Use controller in order to pagename
routes.MapRoute(
name: "Product",
url: "Product/{controller}/{slug}",
defaults: new { controller = "Product", action = "Index", slug = UrlParameter.Optional },
namespaces: new[] { "Demo.Controllers" }
);

Map api route to mvc route

I want my FilesController to be accesible from both /files/{action} and /api/files/{action}. I tried
routes.MapRoute(
name: "Files",
url: "api/Files/{action}/{id}",
defaults: new { controller = "Files", id = UrlParameter.Optional }
);
but it not working. I deleted WebApiConfig, so in App_Start is only MVC RouteConfig, but it doesnt help.
Upd
controller code
public class FilesController : Controller {
public ActionResult Index() {
return new HttpStatusCodeResult(System.Net.HttpStatusCode.OK);
}
}
request urls:
/files/index -> OK (200)
/api/files/index -> Not Found (404)
Upd #2
Complete RouteConfig.cs
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(
name: "FilesRoute",
url: "Files/{action}/{id}",
defaults: new { controller = "Files", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Files",
url: "api/Files/{action}/{id}",
defaults: new { controller = "Files", id = UrlParameter.Optional }
);
}
}
I think you can try to add a new route in front of your original route setting.
This setting will first check URL whether is match Files/{action}/{id} , if not the use api/Files/{action}/{id}
routes.MapRoute(
name: "FilesRoute",
url: "Files/{action}/{id}",
defaults: new { controller = "Files", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Files",
url: "api/Files/{action}/{id}",
defaults: new { controller = "Files", id = UrlParameter.Optional }
);
EDIT
The problem is route setting order you need to set the default route to be the last one.
/api/files/index => Otherwise, the setting will find api controller and files action.
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "FilesRoute",
url: "Home/{action}/{id}",
defaults: new { controller = "Home", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Files",
url: "api/Home/{action}/{id}",
defaults: new { controller = "Home", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}

Route Configuration in MVC

The current Route Config for me is this, which I think is the default one
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
When I write
www.mypage.com
www.mypage.com/home
I get the same page
How Can I make it so that they are two individual pages
www.mypage.com
is the homepage, and
www.mypage.com/home
is another page
www.mypage.com can be handler by a root controller and all the other routes will be handled by the default route.
routes.MapRoute(
name: "Root",
url: "",
defaults: new {controller = "Root", action = "Index"}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { action = "Index", id = UrlParameter.Optional }
);
the explicit defaults allow for the behavior you are currently seeing.
You will still need to create a controller to handle your root site calls
public class RootController : Controller {
public ActionResult Index() {
return View();
}
}
And don't forget to create the related View for your controller.
No need to create a new controller. you can use the same home controller. In Home Controller, create 2 actions - Default and Index. In the routeconfig, use -
routes.MapRoute(
name: "RootDef",
url: "",
defaults: new { controller = "Home", action = "Default", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Set custom parameter in Route

I have 2 routes
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Plugin",
url: "{pluginName}/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I would like to use Html.Action helper and set "pluginName" parameter of my second Route.
I am try to use next code
#Html.Action("Index","Person",new RouteValueDictionary { { "pluginName", "myPlugin" } });
and to get link like
http://mydomain/myplugin/Person/index
but I've getting
http://mydomain/Person/index?pluginName="myPlugin"
How can I get first link pattern?
Register your more specific route first. The routing engine evaluates routes in the order you register them. So if you have a fairly generic route registered early on (which you do), the routing engine will use it and append other values as QueryString parameters (which your seeing).
Try this:
routes.MapRoute(
name: "Plugin",
url: "{pluginName}/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Categories

Resources