• Code
  • Tags
  • Users
  • Titles
  • Log in
  • Feedback
  • FAQ
Share Code
Welcome to ForkCan.com

ForkCan is all about sharing code in a social way.

Discuss, debate or argue with other devs about their or your own code.

Give other devs feedback or make a Fork (Make a better version of a shared code).

Rate the code, if you use the code mark it as used so others can see if the shared code is used by someone.

Help each other to be better devs and to be more productive.


Features not working yet:

Flag a post


QR Code

Tiny Url

http://4kcan.com/s/MjE3

Related Code
Fork of GZIP your Actions in MVC if not used as standard in IIS7 or only use IIS6
Google Analytics helper for ASP.NET Razor
A simple way to cache slow functions in C#
ActionFilter to Handle and Log Error for each Controller, ASP.NET MVC
Fork of Serialize an object into a Querystring, using LINQ syntax
Serialize an object into a Querystring
Make sure the web.config pages/namespace can be used together with ASP.NET MVC 3 P1 and Razor
StringFormat Extension method for HtmlHelper ASP.NET MVC
Get Cookies in Web Api Asp .net Mvc 4 RC
GZIP your Actions in MVC if not used as standard in IIS7 or only use IIS6

Using DynamicObject to get a value from the Request.Params

Get a Request.Params value by taking advantage of the DynamicObject to make the code look more static

4
870 0 2 0 0 0

public static class Helper
{
    public static dynamic Request
    {
        get { return new Request(); }
    }   
}


public class Request : DynamicObject
{
    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        var name = binder.Name.ToLower();

        result = HttpContext.Current.Request.Params[name];

        return true;
    }


    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
        throw new NotSupportedException();
    }
}

Can be used like this for example in Razor:

@Helper.Request.Search

It will display the value of the QueryString or Input field.


Share: twitter | facebook   Action: used | fork | flag

asp.net

Mark 'asp.net' tag as 'like'

Mark 'asp.net' tag as 'ignore'

c#

Mark 'c#' tag as 'like'

Mark 'c#' tag as 'ignore'

dynamic

Mark 'dynamic' tag as 'like'

Mark 'dynamic' tag as 'ignore'

razor

Mark 'razor' tag as 'like'

Mark 'razor' tag as 'ignore'


 @fredrikn "I'm the master"
3.11k
July 23, 2010 3:16 PM

Fork

 Using DynamicObject to get a value from the Request.Params -  @fredrikn Friday 23, 2010 3:16 PM


2 Feedback

Really like it. It gives you much cleaner code since you don´t have to use strings. -  @vimpyboy Friday 23, 2010 3:18 PM
Nice, Razor looks nice the little I have seen. Clean and nice code too :) -  @johannormen Friday 23, 2010 4:47 PM

You must log in before you can give any feedback


0 Discussion(s)

Newest Oldest

You must log in before you can post a comment

Squeed
Made by: Fredrik Normén 2010