0

I have http://example.com/Image/Show/CwYDBw8LDww where example.com is my site, Show is a method in the Image controller, the unique id CwYDBw8LDww is a key in a database which will point to some other URL on a different domain, e.g. a "hosted" image on Google.

The reason for doing this is that the URLs can get horribly long and unwieldy. I am storing the site content as Markdown/HTML and want to be able to write things like:

<p>Here is an image of a cat.</p>    
<img src="http://example.com/Image/Show/CwYDBw8LDww"> 

and have it render the page as if I put:

<img src="http://googleusercontent.com/SomeReallyReallyReallyLongImageURL.jpg">

So in the database there is a table entry with values:("CwYDBw8LDww", "http://googleusercontent.com/SomeReallyLongImageURL.jpg") which is how I know which "really long URL" belongs to the short code.

I know how to (e.g.) print the 'google' URL to the screen by getting an Action to return it, I am already able to talk to the database, etc. The part I'm missing is getting /Images/Show?shortcode=CwYDBw8LDww to bring back the Google image in a way that it can be rendered on the page. And whether I should be doing that in the View, or what.

My View currently has something like:

@Html.Raw(@Model.ArticleContent)

where the content is already stored as <p>Here is an image of a cat</p> etc.

  • I've answered the question myself as I've now got it working. I had got hung up on it being an "image" I embed in a web page, rather than a page redirect at 'top level' i.e. page the browser shows in the address bar etc. Once I'd stepped back and realised a HTTP request is really just a request whatever the "type" of the response I solved it. – seventyeightist Mar 04 '17 at 20:10

1 Answers1

0

I solved it myself with a bit of patience and research! I added the following to my Image controller.

public RedirectResult Show(string shortcode)
{
    return Redirect(data.GetURL(shortcode));
}

where GetURL() is a method in my data access layer which asks the database for the "long" URL that applies to the shortcode. (I handle cases like if the shortcode doesn't exist in the GetURL() code, so Show() assumes it has been given a valid URL.)

This returns a 302 "Temporary Redirect" HTTP response to re-point the client to the 'new' (Google in my example) URL.

This SO question and answers was helpful in getting to the concept I needed.

Community
  • 1
  • 1