locked
C# To Query Based Off Email NOT ID RRS feed

  • Question

  • User-1580140810 posted

    Hi - I want to query based off email address using a C# API to SQL Server.  I have this as my syntax:

    public IEnumerable<testapp> Get()
    {
    	using (TestEntities entities = new TestEntities())
    		return entities.testappes.ToList();
    }
    public testapp Get(string email)
    {
    	using (TestEntities entities = new TestEntities())
    		return entities.testappes.FirstOrDefault(e => e.email == email);
    }

    And if I hit my API like so localhost/api/Test I get all results returned.  But if I hit my API like this localhost/api/Test/email@email.com

    I get this error shown on screen --- this is windows 10 IIS if it matters.  

    HTTP Error 404.0 - Not Found

    The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

    Now I thought that the GET(string email) method I have above would allow me to pass in the email as a parameter to the API, but it is throwing the error instead of giving me my information.  What should I change so this functions properly?

    Monday, November 19, 2018 7:33 PM

All replies

  • User61956409 posted

    Hi,

    Welcome to ASP.NET forums.

    FartsSmellDelicious

    if I hit my API like this localhost/api/Test/email@email.com

    I get this error shown on screen --- this is windows 10 IIS if it matters.  

    HTTP Error 404.0 - Not Found

    The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

    According to your description and code, I can reproduce same issue on my side. I find that the issue is caused by the "." of email address. To solve the problem, you can try to manually add a slash "/" at the end of url, such as “http://localhost:64959/api/test/email@email.com/”.

    Besides, another approach is using URL Rewrite rule, like below.

    <rewrite>
      <rules>
        <rule name="Add trailing slash" stopProcessing="true">
          <match url="^(api/test/.*\.[a-z]{2,4})$" />
          <action type="Rewrite" url="{R:1}/" />
        </rule>
      </rules>
    </rewrite>

    With Regards,

    Fei Han

    Tuesday, November 20, 2018 2:58 AM
  • User-474980206 posted

    the .com in the email make look like a static file. the value should be url encoded:

       localhost/api/Test/email@email%2Ecom

    Tuesday, November 20, 2018 4:00 PM