在webfrom有这段代码,页面Load事件public partial class Default : System.Web.UI.Page
    {
        protected string avatarFlashParam;
        protected string EncodeLocalhost;
        protected string Localhost;
        protected string uid;
        protected void Page_Load(object sender, EventArgs e)
        {
            int port = HttpContext.Current.Request.Url.Port;
            string ApplicationPath = HttpContext.Current.Request.ApplicationPath != "/" ? HttpContext.Current.Request.ApplicationPath : string.Empty;
            uid = "1";
            Localhost = string.Format("{0}://{1}{2}{3}",
                                 HttpContext.Current.Request.Url.Scheme,
                                 HttpContext.Current.Request.Url.Host,
                                 (port == 80 || port == 0) ? "" : ":" + port,
                                 ApplicationPath);
            EncodeLocalhost = HttpUtility.UrlEncode(Localhost);
            avatarFlashParam = string.Format("{0}/images/common/camera.swf?nt=1&inajax=1&appid=1&input={1}&ucapi={2}/Ajax.ashx", Localhost, uid, EncodeLocalhost);
        }
    }现在转成MVC方式,放在ActionController里public ActionResult Index()
    {
        protected string avatarFlashParam;
        protected string EncodeLocalhost;
        protected string Localhost;
        protected string uid;
        protected void Page_Load(object sender, EventArgs e)
        {
            int port = HttpContext.Current.Request.Url.Port;
            string ApplicationPath = HttpContext.Current.Request.ApplicationPath != "/" ? HttpContext.Current.Request.ApplicationPath : string.Empty;
            uid = "1";
            Localhost = string.Format("{0}://{1}{2}{3}",
                                 HttpContext.Current.Request.Url.Scheme,
                                 HttpContext.Current.Request.Url.Host,
                                 (port == 80 || port == 0) ? "" : ":" + port,
                                 ApplicationPath);
            EncodeLocalhost = HttpUtility.UrlEncode(Localhost);
            avatarFlashParam = string.Format("{0}/images/common/camera.swf?nt=1&inajax=1&appid=1&input={1}&ucapi={2}/Ajax.ashx", Localhost, uid, EncodeLocalhost);
        }
    }这个Controller里HttpContext.Current.Request.Url.Port等等都报错了,
该怎么该才能得到webfrom方式的HttpContext里的信息呢?

解决方案 »

  1.   

    MVC没做过,但是这里public ActionResult Index()能这样写吗?
    方法中嵌套方法?
      

  2.   


    sorry,直接拷下来了。
    public ActionResult Index()
        {
            protected string avatarFlashParam;
            protected string EncodeLocalhost;
            protected string Localhost;
            protected string uid;
     
                int port = HttpContext.Current.Request.Url.Port;
                string ApplicationPath = HttpContext.Current.Request.ApplicationPath != "/" ? HttpContext.Current.Request.ApplicationPath : string.Empty;
                uid = "1";
                Localhost = string.Format("{0}://{1}{2}{3}",
                                     HttpContext.Current.Request.Url.Scheme,
                                     HttpContext.Current.Request.Url.Host,
                                     (port == 80 || port == 0) ? "" : ":" + port,
                                     ApplicationPath);
                EncodeLocalhost = HttpUtility.UrlEncode(Localhost);
                avatarFlashParam = string.Format("{0}/images/common/camera.swf?nt=1&inajax=1&appid=1&input={1}&ucapi={2}/Ajax.ashx", Localhost, uid, EncodeLocalhost);    }
    我就是要在Controller里得到HttpContext里的信息,然后传给页面ViewData[avatarFlashParam]那四个参数
      

  3.   

    .net 你多点几下不就出来了
      

  4.   

    参见 Asp.Net MVC / 在Controller中如何得到HttpContext对象http://news.cnblogs.com/group/topic/30991/
      

  5.   

    HttpContext.Current.Request.Url.Port;HttpContext.Request.Url.Port;有神马差别呢?