我正在尝试使用ASP.NET MVC和路由。
似乎MVC强制我在每次想要创建视图时向控制器添加一个公共方法。例如:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
return View();
}
//... a public method for every view.. :(
}
我不想为每个视图创建公共方法。默认情况下,我希望所有视图在系统中的行为为“返回视图()”,除非另有指定。
例如,HTTP GET:
site.com/about site.com/features site.com/ site.com/testimonials site.com/contact-us
就目前而言,我必须添加:
HomeController.About()
HomeController.Features()
HomeController.Index()
HomeController.Testimonials()
HomeController.ContactUs()
所有结果都为“return View()”。这是我的问题,我正在尝试消除为简单视图创建公共操作方法。
对于需要额外处理的视图,例如在HTTP POST上的联系我们页面,请访问:
site.com/contact-us
我想在控制器中特别添加一个方法来发送SMTP消息。
以下是我试图做的更简洁的示例:
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()
}
}
Thanks, Brian