0

I am strugling with creation of url that will be user and seo friendly. I have controller HelloWorld :

public class HelloWorldController : Controller
{
    // 
    // GET: /HelloWorld/ 

    public string Index() 
    { 
        return "This is my <b>default</b> action..."; 
    } 

    // 
    // GET: /HelloWorld/Welcome/ 

    public string Welcome(string name, int numTimes = 1)
    {
        return HttpUtility.HtmlEncode("Hello " + name + ", NumTimes is: " + numTimes);
    }

}

Action welcome needs parameters to be passed like this

localhost:46963/HelloWorld/Welcome?name=Marek&numTimes=5

I want to ask how it is possible to pass parameters by user friendly url for example:

localhost:46963/HelloWorld/Welcome/Marek/5

. I alerady tryed som magic with routes but nothing was working.

Thank you in advance.

CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176
marek_lani
  • 3,895
  • 4
  • 29
  • 50

1 Answers1

1

just write a good route for yourself

  routes.MapRoute(
        "Default",
        "{controller}/{action}/{name}/{id}",
        new { controller = "HelloWorldController", action = "Welcome", name = UrlParameter.Optional,id=UrlParameter.Optional }
    );

the params will be mapped accordingly; these are also called SEO friendly url's

Try this link as well http://weblogs.asp.net/scottgu/archive/2009/10/13/url-routing-with-asp-net-4-web-forms-vs-2010-and-net-4-0-series.aspx

Brij Raj Singh - MSFT
  • 4,903
  • 7
  • 36
  • 55
  • thank you:) is it also possible to restrict usage of url with parameters passed trought ?name=xy. Thank you – marek_lani Aug 16 '13 at 15:39
  • well depends on your choice of url structure, the above mentioned is a SEO friendly url, If I were you I would prefer the SEO friendly url, however using a normal ?= type is really easy to do, you can always skip the params in your route and access them in your code by taking them out from query string; I won't suggest to use it anyways. check out this article for more insight http://www.dotnetcurry.com/ShowArticle.aspx?ID=814 And yes, please mark it as Answer if you feel satisfied. – Brij Raj Singh - MSFT Aug 19 '13 at 05:22