0

One method in my controller returns a view which has link like http://localhost:17000/Questionnaire/CompleteQuestionnaire?GuidToken=4815823D-3BFF-487D-AEB0-BB874AE9FBDE
and other method returns just link like /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);
    }

how can I add root part of link in var questionnaireUrl ?

I tried to add this method in controller

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;
    }

but I got error
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?) How to fix it?

Heidel
  • 3,174
  • 16
  • 52
  • 84
  • I don't think the error occurs in the code you show us. Do you use something like `HttpContextBase.Current` somewhere? In `QuestionnaireRepository.GetById` for example. – Henk Mollema Oct 25 '13 at 08:07
  • @CodeCaster thanks a lot for link, I wrote `var request = System.Web.HttpContext.Current.Request;` and now it works. – Heidel Oct 25 '13 at 08:20
  • You need to add that as the answer to your own question and mark it as such. – greg84 Oct 25 '13 at 08:27

1 Answers1

0

I have found the solution here How can you get the “real” HttpContext within an ASP.NET MVC application?
I changed in my code

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

and now it works.

Community
  • 1
  • 1
Heidel
  • 3,174
  • 16
  • 52
  • 84