我的控制器中的一个方法返回一个视图,其中包含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?)
如何解决?
答案 0 :(得分:0)
我在这里找到了解决方案How can you get the “real” HttpContext within an ASP.NET MVC application?
我改变了我的代码
var request = System.Web.HttpContext.Current.Request;
现在可行。