1

我在我的视图中通过我的 ServiceController 中的 HttpPost 调用 aa 方法http://localhost/Case/Form

我的服务控制器类:

using System.Web;
using System.Web.Mvc;
[...]

namespace ApnNephro.WebUI.Controllers
{
    public class ServicesController : Controller
    {
        public string GetDomain()
        {
            string url = "";
            try
            {
                string _url = Request.Url.AbsolutePath;
                int _si = (_url.IndexOf("//"));
                int _fi = (_url.Substring(_si + 2)).IndexOf("/"); // first /
                url = _url.Substring(0, _fi + _si + 2);
                return url.ToString();
            }
            catch (InvalidOperationException ioe)
            {
                throw new Exception(ioe.Message + " / " + ioe.StackTrace);
            }
            catch (Exception e)
            {    
                throw new Exception(e.Message + " / " + e.StackTrace);
            }
            return url.ToString();
        }

[...]

我在 MailHelper.cs 中给他打电话:

private static string domain = new ServicesController().GetDomain();

但是发生了异常,因为请求为空......

所以,我的问题是,为什么 Request 是 null ?它是否仅在当前视图控制器中分配,所以仅在我的 CaseController 中?

4

1 回答 1

1

您正在创建一个与请求无关的新控制器。无需将获取域放在控制器中然后创建新域,您只需通过HttpContext.Current.Request. 这在静态方法中也可用。

于 2013-09-06T08:42:06.557 回答