-1

I'd like to understand how to set routing parameters to do the follow: when user call shortened url like http://hostname.com/shortenedurl my asp.net mvc project should call action and parameter like http://hostname.com/controller/action
Never before I did something like that so I will be appreciate for any advise. How code and decode url string from normal to short and back I know already.

andrey.shedko
  • 3,128
  • 10
  • 61
  • 121

2 Answers2

1

You can define specific routes in RouteConfig.cs file. For example, .../MyShortUrl will route to the Edit method of EmployeeController

public class RouteConfig
{
  public static void RegisterRoutes(RouteCollection routes)
  {
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
      name: "MyShortUrl",
      url: "MyShortUrl",
      defaults: new { controller = "Employee", action = "Edit", id = UrlParameter.Optional }
    );

    routes.MapRoute(
      name: "Default",
      url: "{controller}/{action}/{id}",
      defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
  }
}

Note specific routes need to placed in order before more general routes

  • Thanks. Is this valid for ../AnyShortUrl? Or just one specified in RouteConfig? – andrey.shedko Jan 30 '15 at 08:03
  • No, if you wanted `../AnyShortUrl` to redirect to a specific controller/action, you would need further routes although you can make use of defaults, e.g you could have `/Employees` go to the `Index` method of `EmployeeController` and `/Projects` go to the Index method of `ProjectController` –  Jan 30 '15 at 08:20
-1

There is no "normal" and "short". It would be best if you used Routing. It's important to read some introduction to Routing to understand how it works.

(I don't think an extract would be appropriate here since the whole article needs to be read and its concepts understood.)

Ofer Zelig
  • 17,068
  • 9
  • 59
  • 93
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – RajeshKdev Jan 30 '15 at 05:15
  • The goal was to point the OP to one of the guides about routing available out there. Giving an answer here doesn't help the OP with their next routing challenges except of some ad-hoc homework answer to the task at hand. It's irrational to shrink a whole resource into an answer so I preferred giving a link. Yes, links change over time, that's why edits are available to fix broken links. – Ofer Zelig Feb 02 '15 at 04:43
  • Hello @Ofer, From the community some one flagged your answer as low quality. Since your answer doesn't met the site standards. Including me and few more people reviewed your answer and categorized as "This is a link-only answer (and not spam)". By the way If you wanna point out something we have "commenting" option for every question, you can make use of it. – RajeshKdev Feb 02 '15 at 06:28
  • It's an answer though. I know the difference between comments and answers (and often I note that myself to users who are just after the points). As you can see in my profile, I'm around here for quite some time now. – Ofer Zelig Feb 02 '15 at 10:58