2

I am currently using the Request.Scheme and Request.Host to composite Uri object to get AbsoluteUri for my .net core MVC application.

Uri location = new Uri($"{Request.Scheme}://{Request.Host}");
string applicationRootURL = location.AbsoluteUri;

But this only works in a non-static method.

As I need to re-use this method in another controller, I am thinking to make this action method static. If I do that, the compiler will complaint about the Request.Scheme and Request.Host.

I am wondering what's other options I have to achieve this?

Thank you.

UPDATE:

This is what I have for ControllerA with ActionMethodA

public class ControllerA
{
    public bool ActionMethodA()
    {
        Uri location = new Uri($"{Request.Scheme}://{Request.Host}");
        string applicationRootURL = location.AbsoluteUri;

        return false;
    }
}

And in another ControllerB, I want to ActionMethodB to invoke ActionMethodA from ControllerA:

public class ControllerB
{
    public void ActionMethodB()
    {
        var result = ActionMethodA();
    }
}

Is creating an Extension Method to the ControllerA is the most proper way to handle this kind of scenario?

Thank you.

Xiao Han
  • 1,004
  • 7
  • 16
  • 33
  • 1
    Make in an extension method of HttpContext... – Tseng Apr 11 '18 at 17:52
  • Sorry, didn't figure out how. But plan to implement the solution without making the method static. – Xiao Han Apr 11 '18 at 21:07
  • It's one of the most basic and most commonly used features in C# https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods – Tseng Apr 11 '18 at 21:08

2 Answers2

1

You can also define an extension method directly for the HttpRequest class and use the BuildAbsolute method of the UriHelper class to build the uri.

public static class HttpRequestExtensions
{
    public static string GetURI(this HttpRequest request)
    {
        return UriHelper.BuildAbsolute(request.Scheme, request.Host);
    }
}

And use it:

public IActionResult ContollerMethod()
{
    var uri = Request.GetURI();

    // your code
}
Andriy Tolstoy
  • 5,690
  • 2
  • 31
  • 30
  • Thanks. What I want is making the ControllerMethod to be static, so in another Controller, I can invoke this ControllerMethod like var result = ControllerMethod(); directly. Is it possible? – Xiao Han Apr 13 '18 at 17:04
  • @XiaoHan There is not an instance reference to a request object in a static method. So I do not think it is possible. – Andriy Tolstoy Apr 16 '18 at 09:29
  • Thank you. Is there any other way I can get application URL within a static method? – Xiao Han Apr 16 '18 at 14:09
  • @XiaoHan Within a parameterless static method? I do not think there is any other way to accomplish it. – Andriy Tolstoy Apr 17 '18 at 07:03
  • Not necessarily, can pass any parameter. – Xiao Han Apr 17 '18 at 18:15
  • @XiaoHan So basically it is certainly possible to define a static method like this one ```public static bool ActionMethodA(HttpRequest request)``` and call it from an another controller with an appropriate request parameter. But even though it works, it is not a right solution, because one controller should not be responsible for a request of another one. – Andriy Tolstoy Apr 18 '18 at 08:06
  • I can isolate this static method into a helper/util class to be able to shared and invoked by multiple controller. But not sure if the URL information can be retrieved within this static method. – Xiao Han Apr 18 '18 at 17:48
  • @XiaoHan Yes, it is also an option. You can get the URL information the same way using ```HttpRequest``` object as a parameter of the static method. Further helpful reading: https://stackoverflow.com/questions/4646328/extension-methods-vs-static-utility-class – Andriy Tolstoy Apr 19 '18 at 07:18
-1

You can write an extension method to a controller or HttpContext object. In the following example I have added an extension method to the controller.

 public static class ControllerExtensions
{
    public static string GetURI(this Controller controller)
    {
        Uri location = new Uri($"{ controller.Request.Scheme}://{controller.Request.Host}");
        string applicationRootURL = location.AbsoluteUri;
        return applicationRootURL;
    }
}

Once the extension method is written you can call it in the following manner.

public IActionResult Index()
    {
        var url = this.GetURI();
        return View();
    }

Make sure to import namespace of an extension method in your calling code

parag
  • 2,483
  • 3
  • 20
  • 34