0

I have a Composite site where the URL structure is like so:

site.com/products/1234/product-name

The bold part is the page location. 1234 is the Product ID and then product-name is just a slug.

I register PathInfo and I'm using 1234 to read a SQL Database and return information to show on the site. I'm doing that inside an inline C# function called productDetails which returns an XElement to the XSLT Function.

Sometimes, we will discontinue a product or the ID will not exist in our database. What I'd like to do in those cases is to do one of the following

  • Return a 404
  • Redirect to my 404 with a URL

I then want to email out a notice (or at least log the error).

Currently, if the product doesn't exist, I just return an empty <product> element.

I have tried the following code inside the InlineMethodFunction > 'productDetails method, but the Response Object doesn't seem to be available. IDEALLY I want to set the Response Type to 404 instead of redirect. Any ideas?:

public static class InlineMethodFunction
{
  public static XElement productDetails( int BulkProductId ) {
    ProductContext db = new ProductContext();

        var products = db.ProductDetailsByBulkID(BulkProductId).ToList();
        if (products.Count < 1)
        {
            // THIS doesn't work
            Response.Redirect("~/404?ProductCode=" + BulkProductId.ToString());

            HttpContext.Current.Response.Redirect("~/404?ProductCode=" + BulkProductId.ToString());
            // ERROR RETURNED The name 'HttpContext' does not exist in the current context
        }

    //
    // rest of code
    //
  }
}
Armstrongest
  • 15,181
  • 13
  • 67
  • 106
  • What is "doesn't work"? Do you mean you want to [show a custom 404 page in ASP.NET without redirect?](http://stackoverflow.com/questions/1367752/how-to-show-a-custom-404-page-in-asp-net-without-redirect)? – CodeCaster May 06 '14 at 21:58
  • Should have been more specific. `The name 'Response' does not exist in the current context`. I think this Composite-C1 specific. – Armstrongest May 06 '14 at 22:05
  • That would indeed be because you're not in a `Page` or `Controller` class that has a `Response` property, but inside a static method of a static class. Can't you [throw a 404](http://stackoverflow.com/questions/1095030/respond-with-404-error-from-asp-net-page-codebehind) and check the question linked in my former comment to handle that with a custom 404? – CodeCaster May 06 '14 at 22:07
  • Thanks! I just tried to throw a 404... unfortunately: `'HttpException' could not be found` – Armstrongest May 06 '14 at 22:13
  • Did you try putting `using System.Web;` on top of your class file? – CodeCaster May 06 '14 at 22:15
  • Yep. That was one of the first things I tried. It may be a Composite-specific issue... I just thought it was something that should be relatively straight-forward. – Armstrongest May 06 '14 at 22:20
  • 1
    I would recommend creating the class and function directly in your Visual Studio and register it as an "external function" withing C1. Much safer and you're in full control. When creating inline functions you give C1 the responsibility of compiling and its hard to know exactly what is going on. – Pauli Østerø May 07 '14 at 06:41

1 Answers1

1

As it turns out, I was only able to accomplish this in an External function. Kudos to Pauli Østerø!

For those interested... here's the actual code used:

if(products.Count < 1){
    HttpResponse response = HttpContext.Current.Response;
    response.StatusCode = 404;
    response.Status = "404 Not Found";

    // 
    // Send mail to let someone know
    //

}

Rather than redirect, I just set the 404 message and the URL stays intact, but with a 404.

Armstrongest
  • 15,181
  • 13
  • 67
  • 106