1

View から ServiceController の HttpPost でメソッドを呼び出しますhttp://localhost/Case/Form

私のserviceControllerクラス:

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 であるため、例外が発生します...

だから、私の質問は、なぜ Request が null なのですか? 現在のView Controllerでのみ割り当てられているので、ここではCaseControllerのみですか?

4

1 に答える 1

1

リクエストに関連付けられていない新しいコントローラーを作成しています。get ドメインをコントローラーに入れてから新しいドメインを作成する代わりに、HttpContext.Current.Request. これは、静的メソッドでも使用できます。

于 2013-09-06T08:42:06.557 に答える