Visual Studio MVC常用技巧总结_第1页
Visual Studio MVC常用技巧总结_第2页
Visual Studio MVC常用技巧总结_第3页
Visual Studio MVC常用技巧总结_第4页
Visual Studio MVC常用技巧总结_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、本文转载:刚用mvc完成了一个小项目,mvc技术又有了一次提升,所以,再次写一点总结性的东西。开发环境:visual studio 2010 rc, mvc 2 rc, entity framework, sql server 20081、不对iis做任何修改,如何在iis6下运行mvc?这个可以参考我前面一篇文章(原创,和微软官方做法不同,可以不修改iis设置就达到目的)传送门:2、不同areas的controller重复导致的问题两个不同的areas会有不同的命名空间,但是会有相同的 controller而在网站maproute的时候却只能识别 controller,因此会出现错误。假设,

2、我在新建一个mvc项目后,直接新建一个areas,并且命名为admin,新建一个home controller运行,弹出以下错误:“/”应用程序中的服务器错误。 the controller name home is ambiguous between the following types:mvcapplication1.controllers.homecontrollermvcapplication1.areas.admin.controllers.homecontroller遇到这种情况,只要这样就可以了context.maproute( web_default, controller/

3、action/id, new controller=home, action = index, id = , new string mvcapplication1.areas.web.controllers );调用 maproute 的时候在后面多传入一个 string ,并且填入你需要 route 的那个 areas 的 controller 所在的命名空间注意!2个地方都要改3、关于filtermvc提供的4个filter很方便,但是有一个问题,filter中不能直接调用 viewdata,tempdata等字段。虽然可以在传入的 filtercontext 中调用到,但是很不方便。这时

4、候,其实可以把4个filter继承一下,自己写一个新的,并在内部放入viewdata等私有字段用 iauthorizationfilter 来举个例子: public abstract class basefilterattribute : filterattribute /这里可以根据自己的喜好来设定 protected httpsessionstatebase session; protected modelstatedictionary state; protected viewdatadictionary viewdata; protected tempdatadictionary t

5、empdata; protected httprequestbase request; protected dictionary routevalues; protected urlhelper url; protected void initialize(controllercontext filtercontext) /初始化 request = filtercontext.requestcontext.httpcontext.request; routevalues = new dictionary(); foreach (var v in filtercontext.requestco

6、ntext.routedata.values) routevalues.add(v.key,v.value.tostring(); viewdata = filtercontext.controller.viewdata; tempdata = filtercontext.controller.tempdata; state = viewdata.modelstate; session = filtercontext.requestcontext.httpcontext.session; url = new urlhelper(filtercontext.requestcontext); pu

7、blic abstract class authorizationfilter : basefilterattribute, iauthorizationfilter public void onauthorization(authorizationcontext filtercontext) /调用初始化函数 initialize(filtercontext); onauthorization(filtercontext); /这里把原来的 onauthorization 替换了一下 public abstract void onauthorization(authorizationcont

8、ext filtercontext); 然后需要使用 iauthorizationfilter 的时候只要继承上面的 authorizationfilter 即可4、mvc中如何实现弹出一个javascript对话框,并且跳转?大家觉得mvc的架构变了,但其实原理和a一样,还是用 response来输出数据所以,只要在 action 的 return 函数前调用 response.write(text); 即可实现。其实和以前一样,下面举个例子,在一个页面中弹出一个对话框后再跳转到别的页面 public actionresult test() /弹出对话框 response.write(ale

9、rt(test);); /跳转到index response.write(window.location.href= + url.action(index) + ;); return null; 5、web.debug.config 和 web.release.config 的用法利用 web.config, web.debug.config, web.release.config可以在不同环境下生成3中不同的web.config版本在vs中调试的时候,直接使用web.config用debug发布的时候,使用web.debug.config用release发布的时候,使用web.release

10、.config然后,这三个文件怎么用呢?你可以实现 web.config 存在一个字段,然后当发布的时候用 web.debug.config 内的字段替换掉也可以本来不存在,发布的时候添加更可以本来存在,发布的时候删除这里就做简单的介绍,介绍一种替代的方法:代码 /web.config下,假设有这个字段 /在web.debug.config下 这样,在用debug发布的时候,那个字段就会被替换掉,具体用法在web.debug.config文件内写了一个网站,上面有全部的语法6、mvc中捕捉错误mvc中,有一个filter可以捕捉错误,但是它的用法是利用attribute来实现的,而且只能加在c

11、ontroller和action上,所以不能捕捉别出的错误其实理论上所有的错误肯定产生于controller中,但有2种情况下,就不会被捕捉了1、页面不存在的时候,找不到对应的controller,那没有任何controller被执行,所以自然也不会捕捉到错误了2、在 iauthorizationfilter 下发生错误的时候,错误捕捉代码在iexceptionfilter中,而iauthorizationfilter的优先权高于iexceptionfilter,所以也就捕捉不到了那有没有别的方法?参考了一个老外的代码,发现了一种完美的方法 protected void application

12、_error(object sender, eventargs e) exception exception = server.getlasterror(); response.clear(); httpexception httpexception = exception as httpexception; routedata routedata = new routedata(); routedata.values.add(controller, error); if (httpexception = null) routedata.values.add(action, index); e

13、lse /its an http exception, lets handle it. switch (httpexception.gethttpcode() case 404: / page not found. routedata.values.add(action, httperror404); break; case 500: / server error. routedata.values.add(action, httperror500); break; / here you can handle views to other error codes. / i choose a g

14、eneral error template default: routedata.values.add(action, general); break; / pass exception details to the target error view. routedata.values.add(error, exception.message); / clear the error on server. server.clearerror(); / call target controller and pass the routedata. icontroller errorcontroll

15、er = new web.controllers.errorcontroller(); errorcontroller.execute(new requestcontext( new httpcontextwrapper(context), routedata); 把这段代码放到 global.asax 中,并且新建一个 controller 叫做 errornamespace web.controllers public class errorcontroller : controller public actionresult index(string error) viewdatatit

16、le = website 网站内部错误; viewdatadescription = error; return view(index); public actionresult httperror404(string error) viewdatatitle = http 404- 无法找到文件; viewdatadescription = error; return view(index); public actionresult httperror500(string error) viewdatatitle = http 500 - 内部服务器错误; viewdatadescription = error; return view(index); publi

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论