ASP.NET Core:获取URL参数但不触发构造函数

huangapple go评论52阅读模式
英文:

ASP.NET Core: getting url params not firing constructor

问题

在我的一个页面中,我创建下一页的链接如下:

<div class="joboffer" onclick="location.href='/Jobs/JobOverview?id=@elem.Id';" style="cursor: pointer;">
    <p>@elem.Title</p>
</div>

这会打开正确的页面。

这个页面有一个构造函数,看起来像这样:

public async Task<IActionResult> OnGetid(string id)
{
    if (!_2_LogIn.IsLoggedIn)
    {
        await Response.WriteAsync("<script>alert('Nicht so schnell!!!')</script>");
        return BadRequest();
    }
    else
    {
        // 获取URL参数
        return Page();
    }
}

不幸的是,尽管页面是通过URL "?id=xxxxx" 打开的,但应该捕获参数的构造函数从未被调用。

为什么会这样?

最好的,

J

英文:

In one of my pages I am creating the link to the next page like this:

 &lt;div class=&quot;joboffer&quot; onclick=&quot;location.href=&#39;/Jobs/JobOverview?id=@elem.Id&#39;;&quot; style=&quot;cursor: pointer;&quot;&gt;
     &lt;p&gt;@elem.Title&lt;/p&gt;
 &lt;/div&gt;

This opens the correct page.

This page has a constructor which looks like this:

public async Task&lt;IActionResult&gt; OnGetid(string id)
{
    if (!_2_LogIn.IsLoggedIn)
    {
        await Response.WriteAsync(&quot;&lt;script&gt;alert(&#39;Nicht so schnell!!!&#39;)&lt;/script&gt;&quot;);
        return BadRequest();
    }
    else
    {
        // gets the url params 
        return Page();
    }
}

Unfortunately, even though the page is opened with the url &quot;?id=xxxxx&quot;, the constructor that is supposed to catch the params is never called.

Why is that?

Best,

J

答案1

得分: 0

From your code, OnGetId method is not a constructor, it seems like an OnGet method in a Razor page.

For example, I have this method in my Privacy page:

public async Task<IActionResult> OnGetId(string id)
{
    if (!_2_LogIn.IsLoggedIn)
    {
        await Response.WriteAsync("<script>alert('Nicht so schnell!!!')</script>");
        return BadRequest();
    }
    else
    {
        // gets the URL params
        return Page();
    }
}

If I want to pass a value to this method via the route, I can use:

<a class="nav-link text-dark" asp-page="/Privacy" asp-page-handler="Id" asp-route-id="xxx">Privacy</a>

Or

<div class="joboffer" onclick="location.href='/Privacy?id=xxx&amp;handler=Id';" style="cursor: pointer;">
    Privacy
</div>

to pass the value.

英文:

From your code, OnGetId method is not a constructor, it seems like a OnGet method in razor page.

For example, I have this method in my Privacy page:

public async Task&lt;IActionResult&gt; OnGetId(string id)
{
    if (!_2_LogIn.IsLoggedIn)
    {
        await Response.WriteAsync(&quot;&lt;script&gt;alert(&#39;Nicht so schnell!!!&#39;)&lt;/script&gt;&quot;);
        return BadRequest();
    }
    else
    {
        // gets the url params 
        return Page();
    }
}

If I want to pass value to this method via route, I can use:

&lt;a class=&quot;nav-link text-dark&quot; asp-page=&quot;/Privacy&quot; asp-page-handler=&quot;Id&quot; asp-route-id=&quot;xxx&quot;&gt;Privacy&lt;/a&gt;

Or

&lt;div class=&quot;joboffer&quot; onclick=&quot;location.href=&#39;/Privacy?id=xxx&amp;handler=Id&#39;;&quot; style=&quot;cursor: pointer;&quot;&gt;
    Privacy
&lt;/div&gt;

to pass value.

huangapple
  • 本文由 发表于 2023年5月22日 23:24:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76307714.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定