Jesse
Jesse

Reputation: 599

ASP.NET: Path in place of URL arguments

I've been taking URL arguments in ASP.NET like so:

www.mysite.com/thread.php?id=123

However, I'd like to use the cleaner method that you often see which looks like:

www.mysite.com/thread/123

How can I do this (get the arguments) in ASP.NET? What's the usual procedure for setting up a system like this?

Upvotes: 1

Views: 164

Answers (3)

SushiGuy
SushiGuy

Reputation: 1637

Path style arguments can be accessed in C# controller like so. Path argument to be retrieved is "id" which returns a value of "123".

MVC Routing

public static void RegisterRoutes(RouteCollection routes)
{
    routes.MapRoute(
        "Default",
        "{controller}/{action}/{id}",
        new { id = RouteParameter.Optional });
}

//Test URL: http://localhost/MyProject/Test/GetMyId/123

MyController.cs

public class TestController : Controller
{
    public string GetMyId()
    {
        //ANSWER -> ControllerContext.RouteData.Values["id"]
        return ControllerContext.RouteData.Values["id"].ToString();
    }
}

Upvotes: 0

Nate
Nate

Reputation: 30656

What that is called, is Url Rewriting. If you are using the ASP.NET-MVC Framework, you get this behavior by default, along with a design pattern that helps make developing it easier.

If you're trying to shoehorn this onto an existing application, I recommend that you look into some url rewriting modules.

Upvotes: 1

Chris B. Behrens
Chris B. Behrens

Reputation: 6295

If I understand you correctly, this is what you're looking for:

Asp.Net URL Routing

Upvotes: 1

Related Questions