如何将URL从操作过滤器传递到主控制器

时间:2019-01-15 17:57:47

标签: c# asp.net asp.net-web-api

在我的操作过滤器中,我通过此代码获取用户访问的URL。

MyActionFilter:

public class LogActionFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        String originalPath = new Uri(HttpContext.Current.Request.Url.AbsoluteUri).OriginalString;

    }
}

我需要将originalPath传递给主控制器,以便将URL保存到db中。如何将网址从操作过滤器传递到主控制器?

主控制器:

public class HomeController : Controller
{

    private readonly MyLogEntities _db = new MyLogEntities();
   [LogActionFilter]
    public ActionResult Index()
    {
        return View();
    }
    [LogActionFilter]
    public ActionResult About(int id)
    {
        ViewBag.Message = "Your application description page.";
        var usr = _db.UserLogs.SingleOrDefault(u => u.ID == id);
        var inputUser = ConfigurationManager.AppSettings["INPUT_USER"].ToString().ToLower();
        usr.Date = DateTime.Now;
        usr.url= originalPath; // need to get this from action filter
        usr.UserName = inputUser;
        _db.Logs.Add(usr);
        _db.Entry(usr).State = EntityState.Added;
        _db.SaveChanges();
        return View();
    }
    public ActionResult Contact()
    {
        ViewBag.Message = "Your contact page.";

        return View();
    }
}

0 个答案:

没有答案