I want to create a URLs shortener website. URLs that I offer are like example.com/XXX where
XXX is the value of the short URL.
I want to have website on example.com and URLs are example.com/xxx. I want to get xxx from URL and redirect users to equivalent URLs in database.
How can implement this?
Create a new route in your RouteConfig for example:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("empty",
"{id}",
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 }
);
}
}
And the simply go to your database with the id passed in the index
public ActionResult Index(int id)
{
//Do Stuff with db
return View();
}
asp.net mvc documentation here.
One way you be doing the needed redirection in the default controller action. By default in asp.net mvc it is home/index.
So in the index action you should be having such code
public ActionResult Index(string id)
{
var url = Db.GetNeededUrl(id);
return Redirect(url);
}
So now if the user enters such an address site.com/NewYear you'll get redirected to the equivalent url that is in you database.
Related
Check the code bellow. Here in route i want to give user enter input of username in url just like example.com/username but problem with that RouteConfig.cs is this cant take input like that. This will only take controller/method format. Please advice me how can i achieve domain/username type input form user? I want to serve that request from Test method bellow
Currently RouteConfig.cs file:
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 }
);
}
}
Test method:
public ActionResult Test(String username)
{
return View();
}
Try this.
routes.MapRoute("Id"
, "{id}"
, new { controller = "Home", action = "Test"});
I wrote a very simple web app in Flask and am porting it to ASP.NET Framework. All the functionality is in JavaScript and HTMl, so the framework should just act as scaffolding. I've got almost everything ported over, except for what seems to be a routing issue. My site expects a string token variable to be appended to the URL, like so: www.mysite.com/token-string. For development, the URL is localhost:*****/string-token, with my Index.cshtml page being displayed as default.
When I pass the URL without the token it works fine and my index page loads. However I get a 404 when I try it with the token. I'm assuming it's identifying the token as a route and is trying to navigate to it? I'm not sure how to fix it. Here are the important parts of my code:
HomeController.cs:
public class HomeController : Controller
{
public ActionResult Index(string token)
{
return View();
}
}
RouteConfig.cs:
NB: I've not changed this, not sure what to do with it.
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 }
);
}
}
It's quite important that the token is passed in the way it is, rather than as a ? query parameter or anything like that. Additionally, the C# index view doesn't really need to do anything with the token - it gets extracted by the JavaScript.
Any advice is most welcome. Thanks.
Each segment (i.e. {controller}) in the route is a variable, and in the default route makes them all optional. Therefore, your default route is matching the request www.mysite.com/token-string.
What you need to do is insert a route that has a constraint to only match URLs with your token. Assuming your token is a GUID, you could use a regex route constraint as follows:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "TokenRoute",
url: "{token}",
defaults: new { controller = "Home", action = "Index" },
constraints: new { token = #"^[0-9A-Fa-f]{8}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{12}$" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
If your token is not a GUID, you could either use a different regex or implement IRouteConstraint to ensure the route only matches your tokens. The logic you use could be as simple as a == statement (as shown) or more complex (such as a database lookup).
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "TokenRoute",
url: "{token}",
defaults: new { controller = "Home", action = "Index" },
constraints: new { token = new TokenConstraint() }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
public class TokenConstraint : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
if ((string)values[parameterName] == "MyToken")
{
return true;
}
return false;
}
}
Note that you should use the route value key {token} in the url: parameter to match the action method parameter name token.
public ActionResult Index(string token)
{
return View();
}
I guess you could try changing the default route to include token instead of id as shown below.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{token}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
The default Route pattern which you have expects the parameter with name as 'id'
Either add (or modify the default route) like below route pattern
routes.MapRoute(
name: "AnotherRoute", //your desired route name
url: "{controller}/{action}/{token-string}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I need to redirect on different action based on role. I have made following changes to RouteConfig.cs.
RouteConfig.cs
routes.MapRoute(
name: "borrower",
url: "borrower",
defaults: new { controller = "Home", action = "Borrower" });
routes.MapRoute(
name: "broker",
url: "broker",
defaults: new { controller = "Home", action = "Broker" });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
Here is my controller
HomeController.cs
public class HomeController : Controller
{
public ActionResult Index()
{
if (UserPrincipal.Current.Identity.IsAuthenticated)
{
if (UserPrincipal.Current.IsInRole("Broker"))
{
return RedirectToRoute("broker");
}
else if (UserPrincipal.Current.IsInRole("Borrower"))
{
return RedirectToRoute("borrower");
}
}
return View();
}
public ActionResult Broker()
{
return View();
}
public ActionResult Borrower()
{
return View();
}
}
Code flow is working properly till return RedirectToRoute("")but after this code flow never goes to related/appropriate action and empty view is returned.
NOTE: Both Broker and Borrower views are not empty they have static text.
In network tab you can see proper redirect is being performed and URL in browser is being set to route URL.
RedirectToRoute("Broker") result in 302 response and user is redirected to abc.com/broker as it should but abc.com/broker does not hit Broker action method in HomeController and empty view is returned. According to RouteConfig.cs it should hit Broker action method.
Please point out what I'm doing wrong here.
It turned out that I had a directory named "Borker" and "Borrower" at root of the web project. Due to this MVC routing module was being ignored and requests (abc.com/broker and abc.com/borrower) was moved to these directory. So I renamed these directories and everything worked as expected :)
A simple routing scenario is not working for me.
my route registration looks like this
context.MapRoute(
"Users_default",
"Users/{controller}/{action}/{id}",
new { action = "Index", id= UrlParameter.Optional });
and i am expecting it to honor the requests for
users/profile/
users/profile/1
users/profile/2
with the following controller
public class ProfileController : Controller
{
public ActionResult Index(int? id)
{
var user = id == null ? (UserModel)HttpContext.Session["CurrentUser"] : userManager.GetUserById((int)id);
return View(user);
}
}
it works for users/profile but not for users/profile/1
i've tried few different things but i know the answer must be simple, its just my lack of knowledge, what am i missing here.
i dont want index to appear. i want to use the same method for both users/profile/1 and users/profile/
Then don't put action into your URL.
context.MapRoute(
"Users_default",
"Users/{controller}/{id}",
new { action = "Index", id= UrlParameter.Optional });
The route you have defined will not allow index to be optional because it is followed by another parameter (in this case "id"). Only the last parameter can be optional on all but the default route.
This is because your route interprets as:
{controller: "profile", action: "1"}.
You need to point you details action url explicit, something like this:
users/profile/index/1
You can use Attribute routing
The code would look like
public class ProfileController : Controller
{
[Route("users/profile/{id}")]
public ActionResult Index(int? id)
{
var user = id == null ? (UserModel)HttpContext.Session["CurrentUser"] : userManager.GetUserById((int)id);
return View();
}
}
And you have to modify your RouteConfig
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// This will enable attribute routing in your project
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
So now you can use users/profile for your default behaviour and users/profile/ for a specific profile.
I need to create a custom route with MVC to build a custom search:
Default Home's Index's Action:
public virtual ActionResult Index()
{
return View();
}
And I would like to read a string like this:
public virtual ActionResult Index(string name)
{
// call a service with the string name
return View();
}
For an URL like:
www.company.com/name
The problem is that I have this two routes but it's not working:
routes.MapRoute(
name: "Name",
url: "{name}",
defaults: new { controller = "Home", action = "Index", name = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", area = "", id = UrlParameter.Optional }
);
This way, any controller other then Home is redirected to Home, and if I switch the order, it can't find the Index Home Action.
You can use attribute routing for this. You can see the details about the package here. After installing the attribute routing package. Now a days it is installed by default for MVC5 application. Change your action method as below:
[GET("{name}")]
public virtual ActionResult Index(string name)
{
// call a service with the string name
return View();
}
Here's a catch for the code above:
The router will send the requests without the name also to this action method. To avoid that, you can change it as below:
[GET("{name}")]
public virtual ActionResult Index2(string name)
{
// call a service with the string name
return View("Index");
}