简体   繁体   English

剃刀页面自定义页面路由

[英]Razor Pages Custom Page Routing

This question has been asked earlier and as I am new to Razor Pages , unable to figure out how I can make the required (Silly one). 这个问题已经在较早时候问过了,因为我是Razor Pages新手,无法弄清楚我该如何制作所需的东西(傻傻的)。 So here it's: I've a razor page where I show a list of data where I've to make the page routing or url as follows: 所以这是:我有一个razor page ,在其中显示数据列表,这些数据使我必须按如下所示进行页面路由或url:

@foreach (var item in Model.Data)
{
  <a href="./search_details/id/@item.Id">@item.Name @item.City @item.State</a>
} 

So pretty simple, so in the Startup.cs , I tried to do the following to make it work. 非常简单,因此在Startup.cs ,我尝试执行以下操作以使其正常运行。 Unfortunately failed: 不幸的是失败了:

//This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    services.Configure<CookiePolicyOptions>(options =>
    {
        // This lambda determines whether user consent for non-essential cookies is needed for a given request.
        options.CheckConsentNeeded = context => true;
        options.MinimumSameSitePolicy = SameSiteMode.None;
    });

    services.AddMvc();
    services.AddEntityFrameworkSqlite().AddDbContext<MyDbContext>();
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    services.AddMvc().AddRazorPagesOptions(options =>
    {
        //Here is the configured routing that I tried
        options.Conventions.AddPageRoute("/search_details", "/search_details/id");
    });
}

I would like to return the url as ./search_details/id/1002 and in the back-end, did this to get the query string value: 我想将网址返回为./search_details/id/1002,并在后端执行此操作以获取查询字符串值:

string id = this.RouteData.Values["id"].ToString();

This is pretty basic, but as I said failed ( Failed means in the back-end, the query string doesn't hit while debugging). 这是非常基本的,但是正如我所说的失败( 失败意味着在后端,调试时查询字符串不会命中)。 I've one more scenario and this thing is bothering me right now the most. 我还有另外一种情况,这件事现在最困扰我。 My requirement is to hide the page name as well. 我的要求是也隐藏页面名称。 Something like this: 像这样:

localhost:8080/search_details/id/1002

To

localhost:8080/1002

Is there appropriate way where I can accomplish that? 有什么合适的方法可以做到这一点吗? I understand that's a bad practice for other purposes but for the time being, I would go with it. 我知道这对其他目的是一种不好的做法,但就目前而言,我会继续这样做。

NB : I am not willing to do it in the client-end unless there is no alternate. 注意 :除非有其他选择,否则我不愿意在客户端执行此操作。 If possible, better with something server-side using C# . 如果可能的话,最好在服务器端使用C#

You can use Route Templates in Razor pages : 您可以在Razor页面中使用路由模板:

https://www.learnrazorpages.com/razor-pages/routing https://www.learnrazorpages.com/razor-pages/routing

Comment out this line options.Conventions.AddPageRoute , and in your search_details page add nullable parameter : 注释掉这一行options.Conventions.AddPageRoute ,并在您的search_details页面中添加nullable参数:

@page "{id?}"

In cs file , you can get route data like : 在cs文件中,您可以获得如下路线数据:

public void OnGet(string id)
{

}

For the second requirement, you can add template like : 对于第二个要求,您可以添加如下模板:

options.Conventions.AddPageRoute("/search_details", "{id}");

So that localhost:8080/1002 will redirect to search_details page and you will get id as route data also . 这样localhost:8080/1002将重定向到search_details页面,您还将获得id作为路由数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM