简体   繁体   中英

Url.Action does not exist in the current context

I am using ASP.Net Core.

I have a function I am trying to move to a separate class to be called by multiple controllers.

This function woud build a pdf with links inside it. For the links, I use:

string urlMeeting = Url.Action("Detail", "Event", new { id = "meeting-" + MeetingID });

It works perfectly fine in my controller but when I move the function from the controller into a separate class in the same project, I keep getting "The name Url does not exist in the current context" .

I tried adding using Microsoft.AspNetCore.Mvc; but same error.

What did I miss? Why doesn't it complain in my controller and does in my external class?

Because your custom controller does inherit from the controller class. This means u Have access to the Url property in the base class. But when u move the code to a seperate class u dont have access to that property.

A solution to this is to parametrize the function that you are calling with the Url.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM