views:

103

answers:

4

Hi there, Ihaven't done much MVC and still learning the hard way on how to do things.

MVC 1 and C#

The Problem I want to provide a customer with a link such as www.temp.com/redirects/cust100?id=123&url=www.nothere.com

from the URL i know it will go to the controller of "redirects" but there isn't an Action of "cust100". How do i create an ActionResult(or something else) that will grab the action so i could query it against a DB to check it is valid before rerouting them else where on my site?

If i have explained myselft too well please feel free to ask more questions.

Kind regards, Pete

+1  A: 

Your URL should be in the same form as the ones in your Global.asax.cs.

I.e.

"{controller}/{action}/..."

So your URL probably needs to look more like:

"Redirect/ToCustomer/123"

Where Redirect is the controller, ToCustomer is an action method on said controller, and "123" is the "id" parameter supplied to the action method:

public class CustomerController : Controller
{
    public ActionResult ToCustomer(int id)
    {
        ...
    }
}

On the other hand, why not just give them the url for the Detail method on your CustomerController. I.e.:

"http://www.temp.com/Customer/Detail/123"
Neil Barnwell
A: 

You will have to use "RedirectResult" which represents a redirection to a new URL.

return RedirectResult(url);

This should solve your problem.

The following listed types are the available derivations of ActionResult:

1 ContentResult —Represents a text result

2 EmptyResult —Represents no result

3 FileResult —Represents a downloadable file (abstract class)

4 FileContentResult —Represents a downloadable file (with the binary content)

5 FilePathResult —Represents a downloadable file (with a path)

6 FileStreamResult —Represents a downloadable file (with a file stream)

7 HttpUnauthorizedResult —Represents the result of an unauthorized HTTP request

8 JavaScriptResult —Represents a JavaScript script

9 JsonResult —Represents a JavaScript Object Notation (JSON) result that can be used in an AJAX application

10 RedirectResult —Represents a redirection to a new URL

11 RedirectToRouteResult —Represents a result that performs a redirection given a route values dictionary

12 PartialViewResult —Base class used to send a partial view to the response

13 ViewResult —Represents HTML and markup

14 ViewResultBase —Base class used to supply the model to the view and then render the view to the response

15 XmlResult —Action result that serializes the specified object into XML and outputs it to the response stream (provided by the MvcContrib library)

Ravia
A quandary. I'm not sure this answers his question, but is an excellent bit of information.
Neil Barnwell
+3  A: 

Your route:

routes.MapRoute("Redirects",               
               "{controller}/{cust}",
                new {controller = "redirects", action = "Index", cust = "" });     

this would make your url work by sending the paramaters to the index method as the default action:

/redirects/cust100?id=123&url=www.nothere.com

And your your Controller Method:

public ActionResult Index(string cust, int id, string url)     
{     
    // do some DB stuff
    return RedirectResult(url);
};
Mark
+1  A: 

The reason your code is trying to find a cust100 action is that your URL is being matched by the default route:

routes.MapRoute(
  "Default",                                              // Route name
  "{controller}/{action}/{id}",                           // URL with parameters
  new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
); 

Try adding an explicit route before this route in your global.asax file that looks like this:

routes.MapRoute(
  "Redirects",         // Route name
  "redirects/{foo}",  // URL with parameters
  new { controller = "Redirect", action = "Redirect", foo = "" }
); 

This will map any URL of the form /redirects/abc123 to the RedirectController.Redirect(string foo) method, and pass in abc123 (or whatever) as the foo parameter.

Dylan Beattie