-
-
Notifications
You must be signed in to change notification settings - Fork 82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
User agent null not work #73
Comments
I am also getting the same issue Message=Exception : Object reference not set to an instance of an object. I think this line may be causing it in InternetExplorer. |
Solution: Create a Middleware and set string empty for User-Agent. public class FixDetectionMiddleware
{
public FixDetectionMiddleware(RequestDelegate next)
{
_next = next;
}
readonly RequestDelegate _next;
public async Task Invoke(HttpContext context, IHostingEnvironment hostingEnviroment)
{
var userAgent = context.Request.Headers["User-Agent"].FirstOrDefault();
if (userAgent == null)
{
context.Request.Headers["User-Agent"] = string.Empty;
}
await _next.Invoke(context);
}
} Startup public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseMiddleware<FixDetectionMiddleware>();
} |
Why would the UserAgent be null? this is the web standard and also it in the test coverage that i wrote. #76 |
UserAgent is null when the request is from another API (not from the browser), for example: var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = await _httpClient.GetAsync("/url/to/my/private/api"); |
Interesting, but REST is strictly HTTP and subject to RFC7231 So should you do add a useragent to the package header? |
Yes, I could. But I have no control over another APIs (not mine) that consumes my API. |
PR #85 merged and closing this issue. |
When user agent is null, the application not work.
The text was updated successfully, but these errors were encountered: