Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何阻止ASP.NET在GET Action方法中注入对象_C#_Asp.net Core - Fatal编程技术网

C# 如何阻止ASP.NET在GET Action方法中注入对象

C# 如何阻止ASP.NET在GET Action方法中注入对象,c#,asp.net-core,C#,Asp.net Core,我有一个用参数调用的动作方法,其中一个参数是int 动作方法的另一个参数是对象 当我检查action方法时,我看到对象参数并非完全为空, 此对象的UserId属性设置为一个值,,例如model.UserId=82 如何阻止ASP.NET创建对象? 我可以通过将userId原语变量重命名为paramUserId来解决这个问题, 但这并不理想 以下是行动方法: [HttpGet] public async Task<IActionResult> Select(int userId = 0

我有一个用参数调用的动作方法,其中一个参数是
int
动作方法的另一个参数是
对象

当我检查action方法时,我看到
对象
参数并非完全为空, 此对象的UserId属性设置为一个值,
,例如model.UserId=82

如何阻止ASP.NET创建对象? 我可以通过将userId原语变量重命名为paramUserId来解决这个问题, 但这并不理想

以下是行动方法:

[HttpGet]
public async Task<IActionResult> Select(int userId = 0, ObjectModel model = null)
ObjectModel:

   public class ObjectModel
   {
       public int Id { get; set; }
       public int UserId { get; set; }
   }

get请求中的复杂模型以querystring的格式传递,因此当您调用
https://localhost:5001/[Area]/[Controller]/Select?userId=82
,默认模型绑定将自动匹配值绑定的参数(不区分大小写)。如果不想更改int类型参数名称,您可以尝试自定义模型绑定,该绑定基于请求中用户ID的大小写,如下所示:

MyCustomerModelBinder

public class MyCustomerModelBinder:IModelBinder
{
    public  Task BindModelAsync(ModelBindingContext bindingContext)
    {
        if (bindingContext == null)

        {
            throw new ArgumentNullException(nameof(bindingContext));
        }

        var modelResult = new ObjectModel();

        //Get the Query in the request 
        var queryResult =new Dictionary<string, string>();
        var query = bindingContext.HttpContext.Request.Query;
        foreach (var k in query.Keys)
        {
            StringValues v = string.Empty;
            var flag = query.TryGetValue(k, out v);
            if (flag)
            {
                queryResult.Add(k, v);
            }
        }

        // Bind model when UserId exists in the Query
        if (queryResult.ContainsKey("UserId"))
        {
            modelResult.Id =Convert.ToInt32(bindingContext.ValueProvider.GetValue("id").FirstValue);
            modelResult.UserId =Convert.ToInt32(bindingContext.ValueProvider.GetValue("UserId").FirstValue);
            bindingContext.Result = ModelBindingResult.Success(modelResult);
            return Task.CompletedTask;
        }

        modelResult = null;
        bindingContext.Result = ModelBindingResult.Success(modelResult);
        return Task.CompletedTask;
    }
}
公共类MyCustomerModelBinder:IModelBinder
{
公共任务BindModelAsync(ModelBindingContext bindingContext)
{
if(bindingContext==null)
{
抛出新ArgumentNullException(nameof(bindingContext));
}
var modelResult=new ObjectModel();
//在请求中获取查询
var queryResult=新字典();
var query=bindingContext.HttpContext.Request.query;
foreach(query.Keys中的var k)
{
StringValues v=string.Empty;
var flag=query.TryGetValue(k,out v);
国际单项体育联合会(旗)
{
添加(k,v);
}
}
//当查询中存在UserId时绑定模型
if(queryResult.ContainsKey(“UserId”))
{
modelResult.Id=Convert.ToInt32(bindingContext.ValueProvider.GetValue(“Id”).FirstValue);
modelResult.UserId=Convert.ToInt32(bindingContext.ValueProvider.GetValue(“UserId”).FirstValue);
bindingContext.Result=ModelBindingResult.Success(modelResult);
返回Task.CompletedTask;
}
modelResult=null;
bindingContext.Result=ModelBindingResult.Success(modelResult);
返回Task.CompletedTask;
}
}
行动

[HttpGet]
    public async Task<IActionResult> Select(int userId = 0, [ModelBinder(BinderType = typeof(MyCustomerModelBinder))] ObjectModel model = null)
[HttpGet]
公共异步任务选择(int userId=0,[ModelBinder(BinderType=typeof(MyCustomerModelBinder))]ObjectModel=null)

此对象模型的用途是什么?@John其他一些操作方法使用对象重定向到此操作方法。我希望模型为空的所有其他调用;但它不是这样工作的。NET正在注入一个对象。我已经有一段时间没有涉足.NETMVC了,但是您认为正在注入的模型实际上是一个视图模型吗?换句话说,是否会自动创建它以在方法视图中使用,以便(可能)在屏幕上显示数据?是否可以尝试将其显式声明为
[FromBody]
?如果有效,我将添加一个答案。@John状态代码:415;不支持的媒体类型。
[HttpGet]
    public async Task<IActionResult> Select(int userId = 0, [ModelBinder(BinderType = typeof(MyCustomerModelBinder))] ObjectModel model = null)