0

I have the following method from Microsoft:

public static string GetPathByAction(this LinkGenerator generator, 
   HttpContext httpContext, 
   string action = null, 
   string controller = null, 
   object values = null, 
   PathString? pathBase = null, 
   FragmentString fragment = default(FragmentString), 
   LinkOptions options = null);

When I call this method without the last parameter I get the error:

  An expression tree may not contain a call or invocation that uses optional arguments

If a parameter is option why do I get such an error?

LinkGenerator is this class:
https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.routing.linkgenerator?view=aspnetcore-2.2

Update

I am injecting LinkGenerator in a class as follows (I am not getting an error with this code because I am passing all parameters):

public class RequestHandler : IRequestHandler<Request, Response>> {

  private LinkGenerator _linkGenerator;

  public RequestHandler(LinkGenerator linkGenerator) {

    _linkGenerator = linkGenerator;

  } 

  public async Task<Response> Handle(Request request, CancellationToken cancellationToken) {

    List<File> files = getFiles();    

    // WORKS
    var url = _linkGenerator.GetUriByAction(action: "GetByUserId", controller: "FileController", null, "", new HostString());

    // DOES NOT WORK
    var = await files
      .Select(x => new Response {
        Id = x.File.Id,
        Url = _linkGenerator.GetUriByAction(action: "GetByUserId", controller: "FileController", null, "", new HostString())
      }).ToListAsync();

    // Remaining code 

  }

}

public class File { 
  public Int32 Id { get; set; }
  public String Url { get; set; }
}
Miguel Moura
  • 36,732
  • 85
  • 259
  • 481
  • 4
    How are you calling it? Can you include that code? – Ben Jan 21 '19 at 22:33
  • Possible duplicate of [An expression tree may not contain a call or invocation that uses optional arguments](https://stackoverflow.com/questions/12413583/an-expression-tree-may-not-contain-a-call-or-invocation-that-uses-optional-argum) – tymtam Jan 21 '19 at 22:38
  • @Ben Just added an update ... – Miguel Moura Jan 21 '19 at 22:39
  • `I am not getting an error with this code`. Could you include the code that is producing the error? – devNull Jan 21 '19 at 22:50
  • @devNull Try the following: Url = _linkGenerator.GetPathByAction("GetByUserId", "FileController", null, new PathString(), new FragmentString()) – Miguel Moura Jan 21 '19 at 22:52
  • @devNull I removed the part , new LinkOptions() part the end. – Miguel Moura Jan 21 '19 at 22:52
  • I was able to pin point the problem and post a better explanation. It seems when applying the method inside a Linq query I need to pass all parameters in LinkGenerator. If I use it outside Linq query I do not. any idea why? I have no idea what is going on ... – Miguel Moura Jan 22 '19 at 00:10

2 Answers2

1

Referring to: https://github.com/Dresel/RouteLocalization/issues/6 and An expression tree may not contain a call or invocation that uses optional arguments

you cannot use it with default arguments

Djordje Nedovic
  • 559
  • 8
  • 20
0

See also: An expression tree may not contain a call or invocation that uses optional arguments

The simple answer is its not that calling the method without the optional parameter that is causing an error but how you're calling. If you're calling the method within a lambda it will only work if you specify the optional parameters.

  • 1
    I read that post but it is not clear to me ... So I need to pass all arguments even if they are optional? This makes no sense ... Even in Microsoft docs they are marked as Optional. any way around it? – Miguel Moura Jan 21 '19 at 22:40
  • You can call the function outside of an expression tree, or you could create a wrapper that calls the function and use that instead. – SomethingSImple Jan 22 '19 at 17:41