質問

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.

役に立ちましたか?

解決

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top