Skip to content

Commit

Permalink
Added PageModelExtensions.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonsagara committed Dec 28, 2023
1 parent 5f9d969 commit f114db5
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
26 changes: 26 additions & 0 deletions src/Sagara.Core.AspNetCore/Extensions/PageModelExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace Sagara.Core.AspNetCore.Extensions;

public static class PageModelExtensions
{
/// <summary>
/// Send a JsonResult whose object has a redirect property containing the URL that the client JavaScript should
/// redirect the browser to.
/// </summary>
/// <param name="pageModel">The PageModel object.</param>
/// <param name="pageName">The target page name.</param>
/// <param name="pageHandler">The page handler, if any.</param>
/// <param name="values">Route values, if any.</param>
public static JsonResult RedirectToPageJson<TPageModel>(this TPageModel pageModel, string pageName, string? pageHandler = null, object? values = null)
where TPageModel : PageModel
{
Check.NotNull(pageModel);

return new JsonResult(new
{
redirect = pageModel.Url.Page(pageName: pageName, pageHandler: pageHandler, values: values)
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void OnException(ExceptionContext context)
log.AppendLine(CultureInfo.InvariantCulture, $" Raw URL: {context.HttpContext.Request.GetEncodedUrl()}");
}

UnhandledExceptionFilterLogger.UnhandledException(_logger, context.Exception, log.ToString());
_logger.UnhandledException(context.Exception, log.ToString());

// Don't set it to handled. Let it continue through the pipeline.
}
Expand All @@ -82,5 +82,5 @@ public void OnException(ExceptionContext context)
internal static partial class UnhandledExceptionFilterLogger
{
[LoggerMessage(EventId = 0, Level = LogLevel.Error, Message = "{message}")]
public static partial void UnhandledException(ILogger logger, Exception ex, string message);
public static partial void UnhandledException(this ILogger logger, Exception ex, string message);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#### [Sagara.Core.AspNetCore](index.md 'index')
### [Sagara.Core.AspNetCore.Extensions](index.md#Sagara.Core.AspNetCore.Extensions 'Sagara.Core.AspNetCore.Extensions')

## PageModelExtensions Class

```csharp
public static class PageModelExtensions
```

Inheritance [System.Object](https://docs.microsoft.com/en-us/dotnet/api/System.Object 'System.Object') &#129106; PageModelExtensions
### Methods

<a name='Sagara.Core.AspNetCore.Extensions.PageModelExtensions.RedirectToPageJson_TPageModel_(thisTPageModel,string,string,object)'></a>

## PageModelExtensions.RedirectToPageJson<TPageModel>(this TPageModel, string, string, object) Method

Send a JsonResult whose object has a redirect property containing the URL that the client JavaScript should
redirect the browser to.

```csharp
public static Microsoft.AspNetCore.Mvc.JsonResult RedirectToPageJson<TPageModel>(this TPageModel pageModel, string pageName, string? pageHandler=null, object? values=null)
where TPageModel : Microsoft.AspNetCore.Mvc.RazorPages.PageModel;
```
#### Type parameters

<a name='Sagara.Core.AspNetCore.Extensions.PageModelExtensions.RedirectToPageJson_TPageModel_(thisTPageModel,string,string,object).TPageModel'></a>

`TPageModel`
#### Parameters

<a name='Sagara.Core.AspNetCore.Extensions.PageModelExtensions.RedirectToPageJson_TPageModel_(thisTPageModel,string,string,object).pageModel'></a>

`pageModel` [TPageModel](Sagara.Core.AspNetCore.Extensions.PageModelExtensions.md#Sagara.Core.AspNetCore.Extensions.PageModelExtensions.RedirectToPageJson_TPageModel_(thisTPageModel,string,string,object).TPageModel 'Sagara.Core.AspNetCore.Extensions.PageModelExtensions.RedirectToPageJson<TPageModel>(this TPageModel, string, string, object).TPageModel')

The PageModel object.

<a name='Sagara.Core.AspNetCore.Extensions.PageModelExtensions.RedirectToPageJson_TPageModel_(thisTPageModel,string,string,object).pageName'></a>

`pageName` [System.String](https://docs.microsoft.com/en-us/dotnet/api/System.String 'System.String')
The target page name.

<a name='Sagara.Core.AspNetCore.Extensions.PageModelExtensions.RedirectToPageJson_TPageModel_(thisTPageModel,string,string,object).pageHandler'></a>

`pageHandler` [System.String](https://docs.microsoft.com/en-us/dotnet/api/System.String 'System.String')
The page handler, if any.

<a name='Sagara.Core.AspNetCore.Extensions.PageModelExtensions.RedirectToPageJson_TPageModel_(thisTPageModel,string,string,object).values'></a>

`values` [System.Object](https://docs.microsoft.com/en-us/dotnet/api/System.Object 'System.Object')
Route values, if any.

#### Returns
[Microsoft.AspNetCore.Mvc.JsonResult](https://docs.microsoft.com/en-us/dotnet/api/Microsoft.AspNetCore.Mvc.JsonResult 'Microsoft.AspNetCore.Mvc.JsonResult')
7 changes: 7 additions & 0 deletions src/Sagara.Core.AspNetCore/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
## Sagara.Core.AspNetCore Assembly
### Namespaces

<a name='Sagara.Core.AspNetCore.Extensions'></a>

## Sagara.Core.AspNetCore.Extensions Namespace
- **[PageModelExtensions](Sagara.Core.AspNetCore.Extensions.PageModelExtensions.md 'Sagara.Core.AspNetCore.Extensions.PageModelExtensions')** `Class`
- **[RedirectToPageJson&lt;TPageModel&gt;(this TPageModel, string, string, object)](Sagara.Core.AspNetCore.Extensions.PageModelExtensions.md#Sagara.Core.AspNetCore.Extensions.PageModelExtensions.RedirectToPageJson_TPageModel_(thisTPageModel,string,string,object) 'Sagara.Core.AspNetCore.Extensions.PageModelExtensions.RedirectToPageJson<TPageModel>(this TPageModel, string, string, object)')** `Method` Send a JsonResult whose object has a redirect property containing the URL that the client JavaScript should
redirect the browser to.

<a name='Sagara.Core.AspNetCore.Filters'></a>

## Sagara.Core.AspNetCore.Filters Namespace
Expand Down

0 comments on commit f114db5

Please sign in to comment.