1

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?

2
  • It would be helpful to know whether you're using ASP.net MVC or web forms. Commented Aug 15, 2011 at 22:14
  • I'm using ASP.NET, but I may want to switch to MVC. Read my response to Nate's answer. Commented Aug 15, 2011 at 22:18

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I've been meaning to ask this; what's difference between creating an ASP.NET website project and creating an ASP.NET MVC application? Is the latter capable of large, user powered, media streaming websites?
In visual studio, a "website" is just a bunch of aspx files in a folder, in an "application" there is a .prj file that keeps track of what files are in the project.
1

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

Asp.Net URL Routing

1 Comment

This does not require MVC, by the way.
0

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();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.