Different Kinds of Action Results in action in MVC

Hi Friends,

I have just put a small snippet for different kinds of action results in MVC. Detailed sample will be explained in the up coming ASP.Net MVC.

public ActionResult Search(string name)
{
var message = Server.HtmlEncode(name);
return Content(message);
}

//Movie/Titanic
routes.MapRoute(“movie”,
“movie/{name}”,
new { controller = “Movie”, action = “Search”, name=UrlParameter.Optional});

public ActionResult Search(string name=”Titanic”)
{
var message = Server.HtmlEncode(name);
return Content(message);
}

// Redirect Permannent will return http status code 301 and temp redirect will return 302
public ActionResult Search(string name=”Titanic”)
{
var message = Server.HtmlEncode(name);
return RedirectPermanent(“http://myview.rahulnivi.net”);
}

// Redirect To action
public ActionResult Search(string name=”Titanic”)
{
var message = Server.HtmlEncode(name);
return RedirectToAction(“Index”, “Home”);
}
// Redirect To action with values
public ActionResult Search(string name=”Titanic”)
{
var message = Server.HtmlEncode(name);
return RedirectToAction(“Index”, “Home”, new { name=name});
}

// Redirect to Route
public ActionResult Search(string name=”Titanic”)
{
var message = Server.HtmlEncode(name);
return RedirectToRoute(“Default”, new { controller=”Home”, action=”About”});
}

// Return a file
public ActionResult Search(string name=”Titanic”)
{
var message = Server.HtmlEncode(name);
return File(Server.MapPath(“~/Content/Site.css”),”text/css”);
}

//Return JSON result

public ActionResult Search(string name=”Titanic”)
{
var message = Server.HtmlEncode(name);
return Json(new { Message = message, Name = “Rahul Sahay” }, JsonRequestBehavior.AllowGet);
}

Thanks,
Rahul
Happy Coding

1,258 total views, no views today