0

我的控制器中的一种方法返回一个具有类似链接的视图,http://localhost:17000/Questionnaire/CompleteQuestionnaire?GuidToken=4815823D-3BFF-487D-AEB0-BB874AE9FBDE
而其他方法只返回类似链接/Questionnaire/CompleteQuestionnaire?GuidToken=4815823D-3BFF-487D-AEB0-BB874AE9FBDE

[HttpGet]
    public JsonResult ShowUrlQuestionnaire(int id)
    {
        var questionnaire = QuestionnaireRepository.GetById(id);
        var questionnaireUrl = "/Questionnaire/CompleteQuestionnaire?GuidToken=" + questionnaire.QuestionnaireId.ToString();
        return Json(questionnaireUrl, "text/html", System.Text.Encoding.UTF8,
                    JsonRequestBehavior.AllowGet);
    }

如何添加链接的根部分var questionnaireUrl

我试图在控制器中添加这个方法

private string GetBaseUrl()
    {
        var request = HttpContext.Current.Request;
        var appUrl = HttpRuntime.AppDomainAppVirtualPath;

        if (!string.IsNullOrWhiteSpace(appUrl)) appUrl += "/";

        var baseUrl = string.Format("{0}://{1}{2}", request.Url.Scheme, request.Url.Authority, appUrl);

        return baseUrl;
    }

但我收到错误
Error 1 'System.Web.HttpContextBase' does not contain a definition for 'Current' and no extension method 'Current' accepting a first argument of type 'System.Web.HttpContextBase' could be found (are you missing a using directive or an assembly reference?) 如何解决?

4

1 回答 1

0

我在这里找到了解决方案How can you get the "real" HttpContext within an ASP.NET MVC application?
我改变了我的代码

var request = System.Web.HttpContext.Current.Request;

现在它可以工作了。

于 2013-10-25T08:29:57.813 回答