Esempio n. 1
0
    /// <inheritdoc />
    public Task ExecuteAsync(HttpContext httpContext)
    {
        if (!SharedUrlHelper.IsLocalUrl(Url))
        {
            throw new InvalidOperationException("The supplied URL is not local. A URL with an absolute path is considered local if it does not have a host/authority part. URLs using virtual paths ('~/') are also local.");
        }

        var destinationUrl = SharedUrlHelper.Content(httpContext, Url);

        // IsLocalUrl is called to handle URLs starting with '~/'.
        var logger = httpContext.RequestServices.GetRequiredService <ILogger <LocalRedirectResult> >();

        Log.LocalRedirectResultExecuting(logger, destinationUrl);

        if (PreserveMethod)
        {
            httpContext.Response.StatusCode = Permanent
                ? StatusCodes.Status308PermanentRedirect
                : StatusCodes.Status307TemporaryRedirect;
            httpContext.Response.Headers.Location = destinationUrl;
        }
        else
        {
            httpContext.Response.Redirect(destinationUrl, Permanent);
        }

        return(Task.CompletedTask);
    }
Esempio n. 2
0
    /// <inheritdoc />
    public Task ExecuteAsync(HttpContext httpContext)
    {
        ArgumentNullException.ThrowIfNull(httpContext);

        // Creating the logger with a string to preserve the category after the refactoring.
        var loggerFactory = httpContext.RequestServices.GetRequiredService <ILoggerFactory>();
        var logger        = loggerFactory.CreateLogger("Microsoft.AspNetCore.Http.Result.RedirectResult");

        var isLocalUrl = SharedUrlHelper.IsLocalUrl(Url);

        if (AcceptLocalUrlOnly && !isLocalUrl)
        {
            throw new InvalidOperationException("The supplied URL is not local. A URL with an absolute path is considered local if it does not have a host/authority part. URLs using virtual paths ('~/') are also local.");
        }

        // IsLocalUrl is called to handle URLs starting with '~/'.
        var destinationUrl = isLocalUrl ? SharedUrlHelper.Content(httpContext, contentPath: Url) : Url;

        Log.RedirectResultExecuting(logger, destinationUrl);

        if (PreserveMethod)
        {
            httpContext.Response.StatusCode = Permanent
                ? StatusCodes.Status308PermanentRedirect
                : StatusCodes.Status307TemporaryRedirect;
            httpContext.Response.Headers.Location = destinationUrl;
        }
        else
        {
            httpContext.Response.Redirect(destinationUrl, Permanent);
        }

        return(Task.CompletedTask);
    }
Esempio n. 3
0
        /// <inheritdoc />
        public Task ExecuteAsync(HttpContext httpContext)
        {
            var logger = httpContext.RequestServices.GetRequiredService <ILogger <RedirectResult> >();

            // IsLocalUrl is called to handle URLs starting with '~/'.
            var destinationUrl = SharedUrlHelper.IsLocalUrl(Url) ? SharedUrlHelper.Content(httpContext, Url) : Url;

            Log.RedirectResultExecuting(logger, destinationUrl);

            if (PreserveMethod)
            {
                httpContext.Response.StatusCode = Permanent
                    ? StatusCodes.Status308PermanentRedirect
                    : StatusCodes.Status307TemporaryRedirect;
                httpContext.Response.Headers.Location = destinationUrl;
            }
            else
            {
                httpContext.Response.Redirect(destinationUrl, Permanent);
            }

            return(Task.CompletedTask);
        }