2

Being kind of a newb to MVC 4 (or really any of the MVC's for ASP.NET) I cant help but feel theres more to the URL helper than what I'm seeing.

Basically I've read the tutorials on populating the attributes in a controllers methods using a query string in the URL. I dont liek query strings though and prefer a sectioned "folder" like style.

Without much further adu, this is the sample URL:

http://something.com/DataTypes/Search/searchString

this approach is actually pretty safe as there will only ever be single worded searches

I have tried in the DataTypes controller

    [HttpGet]
    public ActionResult Search(String q)
    {
        ViewBag.ProvidedQuery = q;
        return View();
    }

and a few other small variations, right now im just trying to get the string to show up in the view but I dont seem to be getting anything there.

Is there anyway to inject the 3rd string in the url into an attribute? If not, which URL helper class am I supposed to use to acquire the string data in the URL? Even if I have to parse the whole URL manually so be it, i just need to acquire that 3rd element in the URL as a string

Extremely n00b question im sure, but either im not finding this simple guide somewhere, or im not searching google correctly...

RedactedProfile
  • 2,748
  • 6
  • 32
  • 51
  • I don't understand. I see no Url helper at all in your example, and your question appears to have nothing to do with Url Helpers. – Erik Funkenbusch Sep 27 '12 at 02:12
  • Thats what I mean, I dont know what to use to accomplish getting elements of the URL sectioned off by forward slashes. IF THERE EVEN IS ONE, I make no assumptions here, like I said, even if its just some function that gets me the full URL and ill parse it manually, I come from PHP, so im used to $_SERVER['REQUEST_URI'] as a global var to get me the entire URL after the main top level domain – RedactedProfile Sep 27 '12 at 02:15

3 Answers3

3

What you're missing is that the default route parameter name is "id". So you want to do this:

[HttpGet]
public ActionResult Search(String id)
{
    ViewBag.ProvidedQuery = id;
    return View();
}

If you don't want to use the variable name id, then you can modify your Route to look like this:

routes.MapRoute(
    name: "Search",
    url: "DataTypes/Search/{searchString}",
    defaults: new { controller = "DataTypes", action = "Search", 
         searchString = UrlParameter.Optional });

If you don't want the string to be optional, then you can remove the last field from the defaults object.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • BOOM, that did it. Did not know that "id" was required for that to work. So.. what about a 4th string then? http://something.com/DataTypes/View/Data-Type-Slug/10 would that just get injected into a 2nd attribute to the controllers method? – RedactedProfile Sep 27 '12 at 02:24
  • @DJDarkViper - Again, you would need a custom route. You would have to add an additional variable to the url string. Alternatively, you can get all the data that comes after a certain part by using a slug expression (then you can parse the slug yourself). For example "DataTypes/View/{*slug}" then you name your parameter in your action method slug, and everything that comes after View is in the string. – Erik Funkenbusch Sep 27 '12 at 02:30
  • Seems like with getting the ID of 10 as well as the slug as a variable the additional route would be "DataTypes/View/{slug}/{id}" – Turnkey Sep 27 '12 at 12:11
  • @Turnkey - yes, that's what I was saying when I said you had to add an additional variable. – Erik Funkenbusch Sep 27 '12 at 15:12
1

you can use RouteTable.Routes.GetRouteData(new HttpContextWrapper(httpContext)) to get the routedata

String URL to RouteValueDictionary

Community
  • 1
  • 1
hagensoft
  • 1,497
  • 13
  • 13
1

You need to look at the routing in the Global.asax.cs. For example for your case you could add a route to the routes collection like this:

        routes.MapRoute("Search",
            "DataTypes/Search/{q}",
            new { controller = "DataTypes", action = "Search" }
        );

Then the q parameter will automatically get mapped to your action. The default controller mapping is likely mapping it to "id".

Turnkey
  • 9,266
  • 3
  • 27
  • 36