1

What component(s) do I need to implement and how can I hook it into the framework in order to have urls where the query parameters with names containing 2 or more words separated by hyphens?


For example:

I would have this url:

www.mysite.com/resource/i-am-looking-for?parameterOne=foo&parameterTwo=bar

I would like to have it like this:

www.mysite.com/resource/i-am-looking-for?parameter-one=foo&parameter-two=bar

My action would be something like this:

ActionResult DoSomething(string parameterOne, string parameterTwo)

The reason: user friendly urls and consistency


I need to have:

  • the component to integrate seamlessly with the framework URL helpers (Url.Action, Url.RouteUrl etc)
  • the incoming data to be bound to the action parameters (or model)

Is there a diagram where I can see the framework extensibility point in this regard?

Thank you!

Dan
  • 1,555
  • 2
  • 14
  • 30

4 Answers4

0

public ActionResult SomeAction( string Some-Var ) {} is invalid because in C# variable names can not contain hyphens. Underscores ARE allowed, however so this IS valid public ActionResult SomeAction( string Some_Var ) {}

Now, if you relax your need to bind to strongly typed input vars to the action, you can accomplish your goal with Request.QueryString["some-var"] but you will have to handle the type conversion and error handling associated with it.

Wolfie
  • 1,801
  • 1
  • 14
  • 16
0

You can add Custom Value Provider Factory as shown below,

public class MyQueryStringValueProvider : NameValuePairsValueProvider
{
    public QueryStringValueProvider(
       HttpActionContext actionContext, 
       CultureInfo culture)
        : base(
            () =>{ 
               var request = actionContext.ControllerContext;
               foreach(var pair in request
                   .GetQueryNameValuePairs()){
                  yield return new KeyValuePair<String,String)(
                       Transform(pair.Key), pair.Value
                  );
              }, culture)
    {
    }

    private String Transform(String key){
        // just removing all - , as it is case insensitive
        // 
        return key.Replace("-","");
    }


}

And you have to register your provider as shown below,

      ValueProviderFactories.Factories.Add(
          new MyQueryStringValueProvider());

For safe side, you can remove existing QueryStringValueProvider to avoid name conflicts for keys that does not have dash.

For dashes in Action Name, you can refer https://github.com/AtaS/lowercase-dashed-route

Akash Kava
  • 39,066
  • 20
  • 121
  • 167
-1

How about url encoding on client's side?

This way you can call controllers using general way. Please have a look one of the answers regarding that: Encode URL in JavaScript?

Community
  • 1
  • 1
Anton Norko
  • 2,166
  • 1
  • 15
  • 20
  • Well, my question was not about encoding but about how to change the routing system in order to change the url pattern. – Dan Sep 06 '15 at 06:32
  • @user2704 can you provide full request url for better understanding of what you need (general part of url and and dashed parameters in bold) please? – Anton Norko Sep 06 '15 at 12:44
  • @user2704, you need to edit you question with these examples so its clear what you trying to achieve. Its not clear why you would want to do this but you can do it using a custom model binder. –  Nov 05 '15 at 01:33
  • @StephenMuecke ok; that's for incomming data but what about url generation (my first point in the list) – Dan Nov 05 '15 at 09:32
-1

for example: this is your action.

public ActionResult GetNew(int id) 
{
      return View(news.Where(i => i.Id == id).SingleOrDefault());
}

First Step Open App_Start>RouteConfig.cs open in your project.

public class RouteConfig
{
  public static void RegisterRoutes(RouteCollection routes)
   {
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
      routes.MapRoute(
      name: "GetNew",
       url: "news/new-detail/{id}",
      defaults: new { controller = "News", action = "GetNew", id = ""}
       );
   }
}

Now run your project and write your browser http://localhost:….(different for you)/news/new-detail/1

New ID No. 1 will open. I hope it's been helpful

Demster
  • 124
  • 3
  • 5
  • Thank you for your help but you did not understand what I want. I can have a route with hyphens but I want to have the query parameter names with hyphens as well. With your example I can have /news/new-detail/1?parameterOne=2 (even though "parameterOne" is not bound) – Dan Nov 06 '15 at 15:11