Looking for way to access URL results of get method call

David_Ford

Member
Joined
Apr 23, 2017
Messages
12
Programming Experience
10+
I have a search form, and the submit button calls another web page with results that were entered. I need to grab that string from the url request and parse it to create a where statement to send to my database. I've looked online for an answer and keep reaching dead ends. At one time there was a "Current" property that was accessible in one of these modules, but it seems to be gone now:

@using Microsoft.AspNetCore.Http.Extensions;
@using System.Web;
@using System.Net.Http;

I'm using Blazor Server, C# 10, ASP.NET 6. I'm using all C#. No JS. I'm not doing an MVC project or Web API (just to circumvent some answers I got in another group).

I need to know what modules to access with a using statement, and some code that will feed me the URL. If it will parse the URL for me too, that would be great. Otherwise I can do that.

Any help appreciated.
 
Last edited:
At one time there was a "Current" property that was accessible in one of these modules, but it seems to be gone now:
Are you sure it was available in Blazor? I feel like it was accessible in ASP.NET.

Anyway, since it is your Blazor component sending the search query, wouldn't you already have the URL used for the query? Or is that search form out of your control?
 
Are you sure it was available in Blazor? I feel like it was accessible in ASP.NET.

Anyway, since it is your Blazor component sending the search query, wouldn't you already have the URL used for the query? Or is that search form out of your control?
The URL is generated by the GET method on the search form. It puts a name/value pair in the URL for each form field. That is what I need to access/intercept.

I'm new at this so I thought Blazor was part of ASP.NET. I didn't know there was a demarcation. If you could explain that comment, it might help me understand things a little more.
 
My quick Google searches seem to indicate that Blazor has a Navigation manager that gives you access to the current URL that Blazor is seeing. It sounds like that URL you are trying to access isn't even in your Blazor app.

Tell us more about this search form and where it sends the query to.
 
This is the code for the search form





Search form code:
@page "/fetchdataSearch"

@namespace BlazorServerApp.Data

@inject DataAccessService ButtonResp


<PageTitle>People Search</PageTitle>


<h2>People Search</h2>

<hr size=25 />

 
    <form  action="https://localhost:7190/FetchDataEdit" method="get">
        
             <table class="table">
                <thead>
                <tr>
                    <th>FirstName</th>
                    <th>Middle</th>
                    <th>LastName</th>
                    <th>Suffix</th>

                </tr>
                </thead>
        
          
                 <tbody >
                    <tr>
                        <td><input type="text" name="FirstName"  /></td>
                        <td><input type="text" name="MiddleName"  /></td>
                        <td><input type="text" name="LastName"  /></td>
                        <td><input type="text" name="Suffix"  /></td>
                  
                    </tr>
               </tbody>
         </table>
         <table class="table">
        

                <thead>
                    <tr>
                        <th>AddressLine1</th>
                        <th>AddressLine2</th>
                    </tr>
                </thead>

                <tbody>
                     <tr>
                        <td><input type="text" name="AddressLine1"   /></td>
                        <td><input type="text" name="AddressLine2"   /></td>
                    </tr>
                </tbody>
                
         </table>
         <table class="table">
                
                <thead>
                    <tr>
                        <th>City</th>
                        <th>State/Province</th>
                         <th>Country/Region</th>
                    </tr>
                </thead>

                <tbody>
                     <tr>
                        <td><input type="text" name="City"   /></td>
                        <td><input type="text" name="StateProvinceCode"  /></td>
                        <td><input type="text" name="CountryRegionCode"  /></td>

                        <td><input type="submit" name="Update" value="Submit" onclick="Submit" /></td>
                        <td><input type="button" name="Create" value="Cancel" onclick="Cancel"/></td>
                        
                    </tr>
                </tbody>
         </table>
       <hr size=25 />   
    
    </form>
 
This is the page that it calls, where I want to do the parsing and then send on to the code that queries the database:

fetchDataEdit.razor:
@using System;
@using System.Runtime;
@using System.Collections;
@using Microsoft.AspNetCore.Http.Extensions;
@using System.Web;
@using System.Net.Http;
@using System.Collections.Specialized;

@using System.Diagnostics;




@page "/fetchdataEdit"

<PageTitle>People Edit</PageTitle>

@using BlazorServerApp.Data
@using System.Diagnostics

@inject DataAccessService PeopleService

<h2>People Edit</h2>

<hr size=25 />

@if (peopleList == null)
{
    <p><em>Loading...</em></p>
}
else
{
    <form >
        
             @foreach (var person in peopleList)
        
             {<table class="table">
                <thead>
                <tr>
                    <th>FirstName</th>
                    <th>Middle</th>
                    <th>LastName</th>
                    <th>Suffix</th>

                </tr>
                </thead>
        
          
                 <tbody >
                    <tr>
                        <td><input type="text" name="FirstName" value=@person.FirstName /></td>
                        <td><input type="text" name="MiddleName" value=@person.MiddleName /></td>
                        <td><input type="text" name="LastName" value=@person.LastName /></td>
                        <td><input type="text" name="Suffix" value=@person.Suffix /></td>
                  
                    </tr>
               </tbody>
         </table>
         <table class="table">
        

                <thead>
                    <tr>
                        <th>AddressLine1</th>
                        <th>AddressLine2</th>
                    </tr>
                </thead>

                <tbody>
                     <tr>
                        <td><input type="text" name="AddressLine1" width=100 value=@person.AddressLine1 /></td>
                        <td><input type="text" name="AddressLine2" width=20 value=@person.AddressLine2 /></td>
                    </tr>
                </tbody>
                
         </table>
         <table class="table">
                
                <thead>
                    <tr>
                        <th>City</th>
                        <th>State/Province</th>
                         <th>Country/Region</th>
                    </tr>
                </thead>

                <tbody>
                     <tr>
                        <td><input type="text" name="City" width=100 value=@person.City /></td>
                        <td><input type="text" name="StateProvinceCode" width=20 value=@person.StateProvinceCode /></td>
                        <td><input type="text" name="CountryRegionCode" width=20 value=@person.CountryRegionCode /></td>

                        <td><input type="button" name="Update" value="Commit" onclick="MakeFormEditable"/></td>
                        <td><input type="button" name="Create" value="Cancel" onclick="WriteToDB"/></td>
                        
                    </tr>
                </tbody>
         </table>
       <hr size=25 />   
             } 
      

    </form>
}

@code {
    private List <Person>? peopleList;

    protected override async Task OnInitializedAsync()
    {
        string contextStr = "";


        //code to access and parse URL would go here


        if (contextStr.Contains("Fr%")) contextStr = " LastName like 'Fr%' ";
        else contextStr = "";

        peopleList = await PeopleService.GetPeopleList(contextStr);
    }
    

}
 
Alternatively, if you bound a data model to an EditForm you would have the data available to you on the server side without having to parse URL parameters:

 
Back
Top Bottom