DamianEdwards / RazorSlices

Lightweight Razor-based templates for ASP.NET Core without MVC, Razor Pages, or Blazor.
MIT License
343 stars 16 forks source link

Razor Slices doesn't play well with Results<T0, T1> pattern #64

Open naasking opened 5 days ago

naasking commented 5 days ago

I want to define an endpoint that I can ensure only returns either a razor slice or a redirect. In ASP.NET I would normally do something like this:

public static Results<IRazorSliceHttpResult, RedirectHttpResult> Hello() =>
    Results.Extensions.RazorSlice<MyApp.Slices.Hello, Model>(new Mode());

However, Results<T0, T1> constructs instances only via an implicit user-defined conversion. Since Results.Extensions.RazorSlice returns an interface and user-defined implicit conversions do not work on interfaces in C#, this pattern appears to be literally impossible with Razor Slices at the moment.

It could be made compatible by changing Results.Extensions.RazorSlice to return a non-interface type. If RazorSliceHttpResultWrapper could inherit from RazorSliceHttpResult, then that would work as the return type.

naasking commented 2 days ago

To workaround this I basically had to replicate the RazorSliceHttpResultWrapper and all of the supporting logic into a custom wrapper type.