标签: onactionexecuting

如何从基本控制器中的OnActionExecuting重定向?

我尝试了两种方法:Response.Redirect(),它什么都不做,以及在基本控制器内部调用一个返回ActionResult并让它返回RedirectToAction()的新方法......这两种方法都不起作用.

如何从OnActionExecuting方法进行重定向?

c# asp.net-mvc onactionexecuting

178
推荐指数
4
解决办法
9万
查看次数

在mvc中执行操作之前的所有操作中的默认参数

有没有办法为MVC应用程序中的每个操作添加一些默认参数.所以如果从锚点href发送,我可以得到参数的值.

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
      int currentformcontrolid = 0;
       foreach (var parameter in filterContext.ActionParameters)
       {
           if (parameter.Key == "FormControlID")
           {
               currentformcontrolid = Int32.Parse(parameter.Value.ToString());
           }
       }
}
Run Code Online (Sandbox Code Playgroud)

和我的href这样(这是一个AJAX锚定电话):

<a 
  data-ajax="true" 
  data-ajax-method="GET" 
  data-ajax-mode="replace" 
  data-ajax-update="#soapfunction-screens" 
  href="/Patient/Intake?FormContorlID=100003"
  data-islandingpage="False"
  id="InkAlcoholDrug" >
    Alcohol Drug Use 
</a>
Run Code Online (Sandbox Code Playgroud)

和我的动作一样(可以是应用程序中的任何动作,每个动作都应该有该参数 FormControlID

public ActionResult WhatEverAction(int TestID=0)
{

}
Run Code Online (Sandbox Code Playgroud)

OnActionExecuting我只能访问TestID而不是href的参数FormControlID

asp.net asp.net-mvc action-filter onactionexecuting

5
推荐指数
1
解决办法
98
查看次数

从 OnActionExecutionAsync 返回而不执行 asp.net core 中的操作

这里我想从 the 返回custom action filter而不执行controller action methodin asp.net core WEB API

以下是我对样品的要求code

public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
    bool valid=SomeMethod();
    if(valid)
        //executes controller action
    else
        //returns without executing controller action with the custom message (if possible)
}
Run Code Online (Sandbox Code Playgroud)

我搜索并找到了一些相关的问题和答案,但没有任何对我有用。

找到了这个await base.OnActionExecutionAsync(context, next);,但它跳过了剩余的逻辑filters并直接执行,controller action所以对我的场景不起作用。

c# custom-action-filter action-filter onactionexecuting asp.net-core-mvc

5
推荐指数
1
解决办法
1万
查看次数

ASP.NET MVC控制器动作与自定义参数转换?

我想设置一个ASP.NET MVC路由,如下所示:

routes.MapRoute(
  "Default", // Route name
  "{controller}/{action}/{idl}", // URL with parameters
  new { controller = "Home", action = "Index", idl = UrlParameter.Optional } // Parameter defaults
);
Run Code Online (Sandbox Code Playgroud)

路由请求看起来像这样......

Example/GetItems/1,2,3
Run Code Online (Sandbox Code Playgroud)

...到我的控制器动作:

public class ExampleController : Controller
{
    public ActionResult GetItems(List<int> id_list)
    {
        return View();
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是,我如何设置将idlurl参数从stringinto转换为List<int>并调用适当的控制器操作?

我在这里看到了一个相关的问题,用于OnActionExecuting预处理字符串,但没有改变类型.我不认为这对我有用,因为当我OnActionExecuting在我的控制器中覆盖并检查ActionExecutingContext参数时,我看到ActionParameters字典已经有idl一个空值的键 - 大概是从字符串尝试转换为List<int>...这个是我想要控制的路由的一部分.

这可能吗?

c# asp.net asp.net-mvc iis-7 onactionexecuting

3
推荐指数
1
解决办法
3547
查看次数

在OnActionExecuting中获取预期的Action参数类型

问题: 是否可以知道被调用动作所期望的参数类型?例如,我有一些action:

[TestCustomAttr]
public ActionResult TestAction(int a, string b)
{
    ...
Run Code Online (Sandbox Code Playgroud)

TestCustomAttr定义为:

public class TestCustomAttr : System.Web.Mvc.ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        ...
Run Code Online (Sandbox Code Playgroud)

因此,当调用TestAction此处内部时OnActionExecuting,我想知道该TestAction方法所期望的类型.(例如:在这种情况下,有2个预期参数.一个是类型int,另一个是类型string.

实际目的: 实际上我需要更改值QueryString.我已经能够获取查询字符串值(通过HttpContext.Current.Request.QueryString),更改它,然后手动将其添加ActionParametersfilterContext.ActionParameters[key] = updatedValue;

问题: 目前,我尝试将值解析为int,如果它被成功解析,我认为它是一个int,所以我进行需求更改(例如值+ 1),然后将其添加到操作参数,对应其键.

 qsValue = HttpContext.Current.Request.QueryString[someKey].ToString();

 if(Int32.TryParse(qsValue, out intValue))
 {
     //here i assume, expected parameter is of type `int`
 }
 else
 {
     //here i assume, …
Run Code Online (Sandbox Code Playgroud)

c# custom-attributes query-string onactionexecuting asp.net-mvc-4

2
推荐指数
1
解决办法
2646
查看次数