I have a .NET Core attribute for handling expcetions:
public class ExceptionActionAttribute : ExceptionFilterAttribute
{
public override void OnException(ExceptionContext context)
{
.... some code
// this is how you do it in .NET 4 MVC
var exceptionUrl = Url.Action("Test", "Error", new { errorId = errorUid.ToString() });
}
}
I would like to build the Url just like in .NET 4 using Url.Action, is there a way or how is the right way to build it so I can get the right path as in my example.
CodePudding user response:
Read up LinkGenerator Class
.
Defines a contract to generate absolute and related URIs based on endpoint routing.
You can resolve that within the filter and generate links using the current request's HttpContext
public class ExceptionActionAttribute : ExceptionFilterAttribute {
public override void OnException(ExceptionContext context) {
// .... some code
// resolve LinkGenerater
LinkGenerater linkGenerater = context.HttpContext.RequestServices.GetService<LinkGenerater>()
// and use it to generate URLs
var exceptionUrl = linkGenerater.GetUriByAction(context.HttpContext,
action: "Test",
controller: "Error",
values: new { FileId = 1 }
);
//...
}
}