Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.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# 从使用Request[“name”变量的其他控制器调用方法_C#_Asp.net_Asp.net Mvc_Asp.net Mvc 3_Request - Fatal编程技术网

C# 从使用Request[“name”变量的其他控制器调用方法

C# 从使用Request[“name”变量的其他控制器调用方法,c#,asp.net,asp.net-mvc,asp.net-mvc-3,request,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 3,Request,我正在使用Asp.NETMVC。我的HTML代码是: <form action="/MobilesController/SaveMobilesAd"> <input type="text" name="brand" /> <input type="text" name="color" /> <!-- other stuff --> <input type="submit" /> </

我正在使用Asp.NETMVC。我的HTML代码是:

<form action="/MobilesController/SaveMobilesAd">
      <input type="text" name="brand" />
      <input type="text" name="color" />
      <!-- other stuff -->
      <input type="submit" />
</form>
现在要在另一个控制器中使用
MyAd()

public class MobilesController:Controller
{
     AdController ad = new AdContoller();
     public ActionResult SaveMobilesAd()
     {
           //some stuff.
           ad.MyAd();
     }
}
现在的问题是,当我在另一个控制器中调用
MyAd()
时,它会给出异常

对象引用未设置为对象的实例


上请求[“品牌”]
。如何避免此异常或此问题?有其他方法实现此目的吗?

当您调用
AdController ad=new AdContoller()时请求对象,或者更确切地说是实例,不会被共享。
MobilesController
中的
Request
对象与
AdController
中的对象不同。您可以执行以下操作:

public void MyAd(HttpRequestBase externalRequest = null)
{
    HttpRequestBase currentRequest = externalRequest ?? Request;

    string brand = currentRequest ["brand"];
    string color = currentRequest ["color"];
    //...
}
然后通过传递另一个控制器实例的
请求
来调用它(如果需要,即如果不从
AdController
调用):


尽管如此,我对这种方法不太满意。我认为您可以(应该?)重构它,并生成一个通用的静态方法,该方法使用(比如)一个
字典
甚至一个自定义类,并处理数据。

当您调用
AdController ad=new AdContoller()时请求对象,或者更确切地说是实例,不会被共享。
MobilesController
中的
Request
对象与
AdController
中的对象不同。您可以执行以下操作:

public void MyAd(HttpRequestBase externalRequest = null)
{
    HttpRequestBase currentRequest = externalRequest ?? Request;

    string brand = currentRequest ["brand"];
    string color = currentRequest ["color"];
    //...
}
然后通过传递另一个控制器实例的
请求
来调用它(如果需要,即如果不从
AdController
调用):


尽管如此,我对这种方法不太满意。我想你可以(应该?)重构它,并创建一个通用的静态方法,比如说,使用一个
字典
或者甚至一个自定义类,并处理数据。

你有没有为此尝试过使用
模型
?为什么要使用Request。只要上课就行了。在动作参数中传递它的对象您不需要手动绑定值
string brand=Request[“brand”]
。只需使用
brand
属性为该方法或模型定义
brand
参数,默认活页夹即可完成此工作。它会在表单值、查询字符串、路由数据等中查找同名变量,因为在MVC体系结构中,这些变量是致命的。为此,您需要使用适当的MVC OOP。如果您想绑定对象参数以获得一点安全性,请使用model和。如果你想继续这样做,或者只是出于某种原因你真的需要这种方法。查看Andrei的答案。也许这可以帮助您尝试使用
Model
?为什么使用Request。只要上课就行了。在动作参数中传递它的对象您不需要手动绑定值
string brand=Request[“brand”]
。只需使用
brand
属性为该方法或模型定义
brand
参数,默认活页夹即可完成此工作。它会在表单值、查询字符串、路由数据等中查找同名变量,因为在MVC体系结构中,这些变量是致命的。为此,您需要使用适当的MVC OOP。如果您想绑定对象参数以获得一点安全性,请使用model和。如果你想继续这样做,或者只是出于某种原因你真的需要这种方法。看看安德烈的答案。也许这能帮上忙,谢谢!我刚刚用
System.Web.HttpContext.Current.Request
替换了
Request
,谢谢!我刚刚用
System.Web.HttpContext.Current.Request
public class MobilesController: Controller
{
    AdController ad = new AdContoller();
    public ActionResult SaveMobilesAd()
    {
       //some stuff.
       ad.MyAd(Request);
    }
}