0

I am developing an application ASP.NET Core 2.0. i have an attribute class to validation my each api request, which inherit ActionFilterAttribute class. But problem is i am not getting desired request url, the given url is coming with parameter, i need only absoluteUrl like /api/values/get not like /api/values/get/1. i think you'll get clear understanding if you see my below codes.

Api

[Route("api/[controller]")]
public class ValuesController : Controller
{
    [HttpGet("get/{id}"),AuthorizationRequiredAttribute]
    public IEnumerable<string> Get(int id)
    {
        return id;
    }
}

AuthorizationRequiredAttribute

public class AuthorizationRequiredAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext context)
    {
        var url=context.HttpContext.Request.Path.ToString()
    }
}
2
  • note that absolute URL has protocol://domain/path format and you really want to check only part of path. So it is better to edit your question. Commented Apr 4, 2018 at 11:04
  • Thanks @Set, edited Commented Apr 4, 2018 at 11:07

2 Answers 2

1

Take a look at Request.GetDisplayURL. This method will give complete URL. Note that the parameter is part of the URL initiated from the client so it will be shown. You will either need to remove it manually or change your URL scheme to make use of query strings.

Sign up to request clarification or add additional context in comments.

Comments

1

.Contains() method help me to implement logic with given action url

public class AuthorizationRequiredAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext context)
    {
        var url=context.HttpContext.Request.Path.ToString()
        if(url.Contains("/api/values/get/1"))
        {
             //do something
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.