Home > Software design >  WebAPI 404 for a bad url
WebAPI 404 for a bad url

Time:10-01

A survey went out to an email audience with the incorrect url:

www.my-url.com/ 

(I'm omitting the real url, but the problem is the /  at the end.) Since it has gone out what I want to do is to modify the web site to return the correct page. However, I can't seem to make it work. It is a WebAPI site returning ContentResult. So for example I tried this:

      [HttpGet]
      [Route(" ")]
      public async Task<ContentResult> Get(string s)

Which didn't work, I also tried [Route("{s}")] which also didn't work.

There is also a controller with this signature:

        [HttpGet]
        public async Task<ContentResult> Get()

Which is why I have the s parameter to distinguish.

Then I tried to set the 404 page in IIS to redirect to the correct page (visiting the url gives a 404) but that also didn't work -- it, along with all the other things I tried, continued with 404.

Since this is a WebAPI I wonder if there is some special consideration I am not taking into account, or if you have any suggestions.

Using .net 6.

CodePudding user response:

This was a little tricky.

[HttpGet]
//[Route(" ")]            // Ends up needing /%C2%A0
//[Route("{s:minlength(6)}")]  // Doesn't work for special chars
[Route("{6}")]                 // A regex does the trick
public IActionResult Get(string s = "hi")
{
    return Ok("hi");
}


[HttpGet]
public IActionResult Get()
{
    return Ok("hi1");
}
  • Related