Note that there are some explanatory texts on larger screens.

plurals
  1. POPublic method for every view? Default ActionResult and Routes in ASP.MVC
    primarykey
    data
    text
    <p>I'm experimenting with ASP.NET MVC and Routes.</p> <p>It appears MVC forces me to add a public method to the controller any time I want to create a view. For example: </p> <pre><code>public class HomeController : Controller { public ActionResult Index() { return View(); } public ActionResult About() { return View(); } //... a public method for every view.. :( } </code></pre> <p>I do not want to create a public method for every view. By default, I want the behavior of "return View()" for all views in the system, unless otherwise specified.</p> <p>For example, HTTP GET:</p> <pre> site.com/about site.com/features site.com/ site.com/testimonials site.com/contact-us </pre> <p>As it stands now, I would have to add:</p> <pre><code>HomeController.About() HomeController.Features() HomeController.Index() HomeController.Testimonials() HomeController.ContactUs() </code></pre> <p>All result in "return View()". This is my problem, I am trying to eliminate creating public action methods for simple views.</p> <p>For views that require additional processing, like a Contact Us page on an HTTP POST to:</p> <pre> site.com/contact-us </pre> <p>I would like to specifically add a method in the controller to send an SMTP message.</p> <hr> <p>The following is a more concise example of what I am trying to do:</p> <pre><code>public class HomeController{ public ActionResult ShowBasicView(){ //HTTP GET: //site.com/about //site.com/features //site.com/ //site.com/testimonials //All URLs above map to this action return View(); } [AcceptVerbs(HttpVerbs.Post)] public ActionResult ContactUs(FormCollection data){ //HTTP POST: //site.com/contact-us //POST URL maps here. SmtpClient.Send(new MailMessage()) //etc... return View() } } </code></pre> <p>Thanks, Brian</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload