简体   繁体   中英

C# WebAPI redirect to a Link that displays an Image

    public HttpResponseMessage getLogoPath(int ID)
    {
        String urlsubfix = dataManager.getMobileLogoPath(ID);
        String urlToReturn =  PictureController.urlprefix  +urlsubfix;
        var response = Request.CreateResponse(HttpStatusCode.Redirect);
        response.Headers.Location = new Uri(urlToReturn);
        return response;
    }

I want to have one of the API call to return an image, how I am accomplishing that is to redirect to another link that has the image, for example :

http://steve.files.wordpress.com/2006/03/Matrix%20tut%202.jpg

I am trying to use PostMan to test this and it gives me back a broken image. I tried derping around and still couldn't find a solution.

-----Edit part of the code that needs this

    public HttpResponseMessage GetCustomerBarCodeLogo(String ID)
    {
        Barcode BC = new Barcode();
        Image img =BC.Encode(TYPE.CODE128, "123456789");
        HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
        ImageConverter imageConverter = new ImageConverter();
        byte[] resourceByteArray = (byte[])imageConverter.ConvertTo(img, typeof(byte[]));
        MemoryStream dataStream = new MemoryStream(resourceByteArray);
        result.Content = new StreamContent(dataStream);
        result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");
        return result; 
    }

邮递员的结果

This returns a broken image.

------Newest Update

I sort of found out the problem. What happens is that there is a problem with SSL basic authentication. What happens is when the first time the client wants to pass in a request, the authentication was set correctly. However, when it tries to get an image which is another request after that is triggered by internal code, that request does not have autherizationHeader and it is not authenticated. I don't know where is the 2nd event triggered so I don't know how to manually set that header.

You cannot redirect to response an image in this url, your caller would be able to change the location.

First solution, try using directly the image result for web api, for sample:

public HttpResponseMessage getLogoPath(int id)
{
    string pathImage = // a way to get the imagem path..

    // create response
    HttpResponseMessage response = new HttpResponseMessage(); 

    // set the content (image)
    response.Content = new StreamContent(new FileStream(pathImage));

    // set the content type for the respective image
    response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png"); // or jpg, gif..

    return response;
}

Or use Moved Status Code, for smaple:

public HttpResponseMessage getLogoPath(int ID)
{
    String urlsubfix = dataManager.getMobileLogoPath(ID);
    String urlToReturn =  PictureController.urlprefix + urlsubfix;

    var response = Request.CreateResponse(HttpStatusCode.Moved);
    response.Headers.Location = new Uri(urlToReturn);
    return response;
}

Here is an alternate way that might work for you. This is the close the the technique I use.

public HttpResponseMessage GetCustomerBarCodeLogo(String ID)
{
    Barcode BC = new Barcode();
    Image img =BC.Encode(TYPE.CODE128, "123456789");

    var dataStream = new MemoryStream();
    img.Save(dataStream, ImageFormat.Png);
    dataStream.Position = 0;

    var result = new HttpResponseMessage(HttpStatusCode.OK);
    result.Content = new StreamContent(dataStream);
    result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");
    return result; 
}

You can use the FileResult for example

public FileResult GetLogo(string imageName)
{
    return File(Server.MapPath("~/App_Data/Images/") + imageName, "image");
}

In this case the File function takes the file path and content type and returns a file response.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM