tags:

views:

91

answers:

5

I tried to use the Sub New but it I don't have access to the url information

what I have is something like:

 function Index(byval lang as string) as action result
      setLang(lang)
      ....
      return view
 end function

 function List(byval lang as string) as action result
      setLang(lang)
      ....
      return view
 end function


 function Details(byval id as integer?, byval lang as string) as action result
      setLang(lang)
      ....
      return view
 end function

.....

is there a generic way I could use so I don't have to deal with the language in EVERY action?

+1  A: 

You could try to look into Aspect-Oriented Programming (AOP) if you really want to do it the generic way, but I am not entirely sure its worth it in your case.

Random link on AOP: http://weblogs.asp.net/podwysocki/archive/2008/03/28/understanding-aop-in-net.aspx

Tinus
Post Sharp is a good .Net AOP framework : http://www.postsharp.org/
brendan
A: 

The short answer is no.

If you find that you need to do the same thing constantly, you might want to re-think your architecture on a higher level.

StriplingWarrior
+1  A: 

Override OnActionExecuting():

public class YourController : Controller
{
    protected string Lang;

    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        //Lang = filterContext.ActionParameters["lang"];

        /* OR */

        Lang = filterContext.RouteData.Values["lang"];
    }

    ...
}

It is better to override OnActionExecuting() in the base Controller if you have one in your project.

UPDATE:

You can remove "lang" from your route and from your actions and move it to query string:

<%= Html.ActionLink("text", "action", "controller", new { lang = "ru" }, null) %>

with "Default" route will produce:

/controller/action/id?lang=ru

Then in OnActionExecuting:

Lang = Request.QueryString["lang"];
eu-ge-ne
this is working, at least I don't have to execute that function every time but I still have to put the "byval lang as string"
Fredou
Fredou, I've updated my answer
eu-ge-ne
this is working perfectly, I didn't even think about querystring! thanks a lot
Fredou
A: 

If this were in Python I would think about using a function decorator. I see examples of doing this with C# but nothing for VB.NET. In this particular case though, I am not sure there is really any benefit.

lambacck
+1  A: 

I would create a custom action filter and decorate each action method with my custom [HandleLanguage] attribute.

Anthony Conyers