Arbejdsglæde
Arbejdsglæde

Reputation: 14088

How to handle get parameter on any page with asp mvc

Hello I am expecting to get some get parameters from customer request, I need to analyze them and make some action, but I except to get this kind of request to any pages. How to handle it in one place with ASP MVC ? I can use attribute, but I am not sure that this is good solution and I don't wan't to change method signature.

Upvotes: 1

Views: 1230

Answers (4)

higgs.boson
higgs.boson

Reputation: 55

You must use this approach

string param = HttpContext.Current.Request.QueryString["param"];

Upvotes: 1

SridharVenkat
SridharVenkat

Reputation: 656

Yes using Attributes is a good solution. You can use ASP.Net ActionFilters(It uses Attributes). They can help to intercept the call made to action and take a decision further.

Here is a sample code from ASP.Net site -- http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/understanding-action-filters-cs

using System;
using System.Diagnostics;
using System.Web.Mvc;
using System.Web.Routing;

namespace MvcApplication1.ActionFilters
{
     public class LogActionFilter : ActionFilterAttribute

     {
          public override void OnActionExecuting(ActionExecutingContext filterContext)
          {
               Log("OnActionExecuting", filterContext.RouteData);       
          }

          public override void OnActionExecuted(ActionExecutedContext filterContext)
          {
               Log("OnActionExecuted", filterContext.RouteData);       
          }

          public override void OnResultExecuting(ResultExecutingContext filterContext)
          {
               Log("OnResultExecuting", filterContext.RouteData);       
          }

          public override void OnResultExecuted(ResultExecutedContext filterContext)
          {
               Log("OnResultExecuted", filterContext.RouteData);       
          }


          private void Log(string methodName, RouteData routeData)
          {
               var controllerName = routeData.Values["controller"];
               var actionName = routeData.Values["action"];
               var message = String.Format("{0} controller:{1} action:{2}", methodName, controllerName, actionName);
               Debug.WriteLine(message, "Action Filter Log");
          }

     }
}

In your Controller Action method. Decorate the action with LogActionFilter or Decorate the entire Controller with the Filter, in below case the Filter will be called in all Controller's action. I guess this would suit you...

using System.Web.Mvc;
using MvcApplication1.ActionFilters;

namespace MvcApplication1.Controllers
{
     [LogActionFilter]
     public class HomeController : Controller
     {
          public ActionResult Index()
          {
               return View();
          }

          public ActionResult About()
          {

               return View();
          }
     }
}

Upvotes: 1

Erdogan Kurtur
Erdogan Kurtur

Reputation: 3685

I've created a few ActionFilters for task such as this. for example, one checks for Anti Forgery tokens for each POST request, and gives error if one not found or is invalid. So I do not have to check it and redirect to appropriate page on every controller.

Upvotes: 1

Rohit
Rohit

Reputation: 10236

You can use Querysting by adding it to the action method of your signature

Here is an example

public ActionResult YourAction(string param1, string param2)
{
        string _param1= param1;
        string _param2= param2;
        return View();
}

It will accept a query .../YourRoute?param1=value1&param1=value2

in the meanwhile also take a look at QueryString MVC

Upvotes: 0

Related Questions