如何重定向到Razor Pages的父虚拟目录?

时间:2018-06-08 11:54:13

标签: c# asp.net asp.net-core webforms asp.net-core-2.1

我有一个Web应用程序,它由两部分组成,第一部分位于IIS虚拟目录MainFolder/RazorFolder中,第二部分位于虚拟目录MainFolder/Default.aspx中。第二个应用程序是带有Razor Pages的.Net Core Web Application。我有一个页面MainFolder/RazorFolder/MyRazorPage和一个页面default.aspx

当我从MyRazorPage转到Response.Redirect("RazorFolder/MyRazorPage") 时,我使用

return Redirect("http://localhost/MainFolder/Default.aspx");

但我不知道如何取回。唯一的工作方式是(在行动方法中)

return Redirect("~/Default.aspx");

仅在我的测试环境中有效。

其他尝试:

http://localhost/MainFolder/RazorFolder/Default.aspx

重定向到Url.Content("~/Default.aspx")

"/MainFolder/RazorFolder/Default.aspx"

返回@Href("../default.aspx")

Hopefull(见ResolveUrl/ResolveClientUrl equivalents for Asp.Net Razor?

"../default.aspx"

返回apply plugin: 'com.android.application' android { compileSdkVersion 'android-P' defaultConfig { applicationId "com.example.johng.assosfood" minSdkVersion 15 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" vectorDrawables.useSupportLibrary = true } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:28.0.0-alpha1' implementation 'com.android.support.constraint:constraint-layout:1.1.0' implementation 'com.android.support:design:28.0.0-alpha1' implementation 'com.android.support:support-vector-drawable:28.0.0-alpha1' implementation 'com.android.support:support-v4:28.0.0-alpha1' implementation 'com.android.support:recyclerview-v7:28.0.0-alpha1' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' implementation 'com.google.firebase:firebase-core:16.0.0' implementation 'com.google.firebase:firebase-auth:16.0.1' implementation 'com.google.android.gms:play-services-auth:15.0.1' implementation 'com.google.firebase:firebase-database:16.0.1' implementation 'com.aurelhubert:ahbottomnavigation:2.1.0' implementation 'com.google.android.gms:play-services-location:15.0.1' } apply plugin: 'com.google.gms.google-services'

所以我要么需要能够重定向到父虚拟目录,要么获取整页URL来解析它。或者我应该从webforms应用程序中走私(羞耻)返回地址?

1 个答案:

答案 0 :(得分:0)

似乎没有允许使用双点(/ ..)语法的方法。因此,有必要获取绝对URL,对其进行解析,删除不需要的部分并重新构建。

允许完成任务的方法位于Microsoft.AspNetCore.Http.Extensions内核中。

  1. 完整网址:Request..GetDisplayUrl()
  2. 解析它:UriHelper.FromAbsolute
  3. 再次构建已处理的URL UriHelper.BuildAbsolute

因此,完整的扩展方法(请注意,参数parentRelativeUri必须以'/'开头):

public static string GetParentUri(this HttpRequest request, string parentRelativeUri)
{
    string scheme;
    HostString host;
    PathString path;
    QueryString query;
    FragmentString fragment;

    UriHelper.FromAbsolute(request.GetDisplayUrl(), out scheme, out host, out path, out query, out fragment);             

    var pathBase = request.PathBase.ToString();

    return UriHelper.BuildAbsolute(scheme, host, 
        pathBase.Substring(0, pathBase.LastIndexOf("/")),
        parentRelativeUri);
}

最后,重定向到父级是:

return Redirect(Request.GetParentUri("/Default.aspx"));
相关问题