Asp.net mvc4中邮箱账号激活怎么mvc4实现登录功能的??

WebSocket在ASP.NET MVC4中的简单实现
WebSocket在ASP.NET MVC4中的简单实现
20:48 by 小白哥哥, 558 阅读, 3 评论, 收藏, 编辑
WebSocket 规范的目标是在中实现和服务器端双向通信。双向通信可以拓展浏览器上的应用类型,例如实时的数据推送、游戏、聊天等。有了WebSocket,我们就可以通过持久的浏览器和服务器的连接实现实时的数据通信,再也不用傻傻地使用连绵不绝的请求和常轮询的机制了,费时费力,当然WebSocket也不是完美的,当然,WebSocket还需要浏览器的支持,目前IE的版本必须在10以上才支持WebSocket,Chrome Safari的最新版本当然也都支持。本节简单介绍一个在服务器端和浏览器端实现WebSocket通信的简单示例。
1.服务器端
我们需要在MVC4的项目中添加一个WSChatController并继承自ApiController,这也是ASP.NET MVC4种提供的WEB API新特性。
在Get方法中,我们使用HttpContext.AcceptWebSocketRequest方法来创建WebSocket连接:
namespace WebSocketSample.Controllers
& & public class WSChatController : ApiController
& & & & public HttpResponseMessage Get()
& & & & & & if (HttpContext.Current.IsWebSocketRequest)
& & & & & & {
& & & & & & & & HttpContext.Current.AcceptWebSocketRequest(ProcessWSChat);
& & & & & & }
& & & & & & return new HttpResponseMessage(HttpStatusCode.SwitchingProtocols);
& & & & private async Task ProcessWSChat(AspNetWebSocketContext arg)
& & & & & & WebSocket socket = arg.WebS
& & & & & & while (true)
& & & & & & {
& & & & & & & & ArraySegment&byte& buffer = new ArraySegment&byte&(new byte[1024]);
& & & & & & & & WebSocketReceiveResult result = await socket.ReceiveAsync(buffer, CancellationToken.None);
& & & & & & & & if (socket.State == WebSocketState.Open)
& & & & & & & & {
& & & & & & & & & & string message = Encoding.UTF8.GetString(buffer.Array, 0, result.Count);
& & & & & & & & & & string returnMessage = &You send :& + message + &. at& + DateTime.Now.ToLongTimeString();
& & & & & & & & & & buffer = new ArraySegment&byte&(Encoding.UTF8.GetBytes(returnMessage));
& & & & & & & & & & await socket.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None);
& & & & & & & & }
& & & & & & & & else
& & & & & & & & {
& & & & & & & & & &
& & & & & & & & }
& & & & & & }
在这段代码中,只是简单的检查当前连接的状态,如果是打开的,那么拼接了接收到的信息和时间返回给浏览器端。
2.浏览器端
在另外一个视图中,我们使用了原生的WebSocket创建连接,并进行发送数据和关闭连接的操作
& & ViewBag.Title = &Index&;
@Scripts.Render(&~/Scripts/jquery-1.8.2.js&)
&script type=&text/javascript&&
& & & & function () {
& & & & & & $(&#btnConnect&).click(function () {
& & & & & & & & $(&#messageSpan&).text(&Connection...&);
& & & & & & & & ws = new WebSocket(&ws://& + window.location.hostname +&:&+window.location.port+ &/api/WSChat&);
& & & & & & & & ws.onopen = function () {
& & & & & & & & & & $(&#messageSpan&).text(&Connected!&);
& & & & & & & & };
& & & & & & & & ws.onmessage = function (result) {
& & & & & & & & & & $(&#messageSpan&).text(result.data);
& & & & & & & & };
& & & & & & & & ws.onerror = function (error) {
& & & & & & & & & & $(&#messageSpan&).text(error.data);
& & & & & & & & };
& & & & & & & & ws.onclose = function () {
& & & & & & & & & & $(&#messageSpan&).text(&Disconnected!&);
& & & & & & & & };
& & & & & & });
& & & & & & $(&#btnSend&).click(function () {
& & & & & & & & if (ws.readyState == WebSocket.OPEN) {
& & & & & & & & & & ws.send($(&#txtInput&).val());
& & & & & & & & }
& & & & & & & & else {
& & & & & & & & & & $(&messageSpan&).text(&Connection is Closed!&);
& & & & & & & & }
& & & & & & });
& & & & & & $(&#btnDisconnect&).click(function () {
& & & & & & & & ws.close();
& & & & & & });
&fieldset&
& & &input type=&button& value=&Connect& id=&btnConnect&/&
& & &input type=&button& value=&DisConnect& id=&btnDisConnect&/&
& & &input type=&text& id=&txtInput&/&
& & &input type=&button& value=&Send& id=&btnSend&/&
& & &span id=&messageSpan& style=&color:&&&/span&
&/fieldset&
3.测试结果
您对本文章有什么意见或着疑问吗?请到您的关注和建议是我们前行的参考和动力&&
您的浏览器不支持嵌入式框架,或者当前配置为不显示嵌入式框架。ASP.NET MVC4学习笔记之Controller激活的扩展 - 十三 - 推酷
ASP.NET MVC4学习笔记之Controller激活的扩展 - 十三
一. 为什么要进行扩展
在前面的分析中,我们知道默认的Controller激活系统只能实例化无参构造函数的Controller类型,但在某些情况一下,我们希望某些服务的实例能够自动注入到Controller实例中,从而达到服务接口和实现的隔离,减小重复的代码,提高系统的可维护性和灵活性。也就是说我们希望在Controller激活中引入依赖注入。关于依赖注入的概念这里就不解释了,请自行查询相关的资料。基于.net依赖注入框架也有很多,下面的例子主要使用微软企业库的Unity。
在上一节的分析中,我们知道Controller的激活实际是包括获取IControllerFacotry和IController实例, 在这两个级别都可以引入扩展实现在我们的目的,现在具体来看一下。
一.实列化IControllerFactory &Level
在这个Level我们知道了路由系统提供的路由信息,当然甚至可以从头到尾自定义实现一个ControllerFactory,包括Controller类型的确定和实例化,但通常没什么必要。我们目标是在Controller实例化阶段注入服务实例,主要还是在Controller实例化阶段。
二. 实列化Controller Level
在这一阶段,我们已经确定Controller的类型,通过上一节的分析,有三个点我们可以引入依赖注入。我们假设有一个要显示所有Customer信息的页面,在其中应用了仓储模式,在CustomerController实例化进自动注入仓储的实例。
1. 继承DefaultControllerFactory,重写GetControllerInstance方法
。大概的代码如下:
public class UnityControllerFactory : DefaultControllerFactory
    public IUnityContainer Container
      get;
      private set;
    public UnityControllerFactory(IUnityContainer container)
      this.Container =
    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
      return (IController)this.Container.Resolve(controllerType);
然后在Global.asax的Application_Start中注册
private void RegisterUnityControllerFactory()
    IUnityContainer container = new UnityContainer();
    container.RegisterType&ICustomerRepository, CustomerRepository&();
    ControllerBuilder.Current.SetControllerFactory(new UnityControllerFactory(container));
2. 自定义实现IControllerActivator。
大概的代码如下:
public class UnityControllerActivator : IControllerActivator
    public IUnityContainer Container
      get;
      private set;
    public UnityControllerActivator(IUnityContainer c)
      this.Container =
    public IController Create(RequestContext requestContext, Type controllerType)
      return (IController)this.Container.Resolve(controllerType);
 private void RegisterUnityControllerActivator()
    IUnityContainer container = new UnityContainer();
    container.RegisterType&ICustomerRepository, CustomerRepository&();
    ControllerBuilder.Current.SetControllerFactory(new DefaultControllerFactory(new UnityControllerActivator(container)));
3.自定义实现全局的IDependencyResolver
public class UnityDependencyResolver : IDependencyResolver
private IUnityContainer Container
public UnityDependencyResolver()
this.Container = new UnityContainer();
public UnityDependencyResolver RegisterType&TFrom, TTo&() where TTo : TFrom
this.Container.RegisterType&TFrom, TTo&();
return this;
public object GetService(Type serviceType)
return this.Container.Resolve(serviceType);
catch(ResolutionFailedException)
return null;
public IEnumerable&object& GetServices(Type serviceType)
return this.Container.ResolveAll(serviceType);
catch (ResolutionFailedException)
return null;
private void SetDependencyResolver()
UnityDependencyResolver resolver = new UnityDependencyResolver();
resolver.RegisterType&ICustomerRepository, CustomerRepository&();
DependencyResolver.SetResolver(resolver);
三.真的要扩展吗?
天下没有完美的东西,引入一样东西我们必须要充分认识它的优势,也要认清它的副作用。在实际项目中,个人觉得大部分情况是没必要在Controller这个Level引入依赖注入,在Controller的Action中,通常我们要调用业务逻辑服务,业务逻辑的服务接口通常是没必要再引入一层另外的抽象。
另外,在这里大致总结一下,在Action中大概有两种方式来调用业务逻辑,一种利用Command模式,即把每个Action 对一个Command,在Command中再调用业务逻辑服务.一种是每个Controller包含一个或多个粗粒度的业务包装服务类,每个Action中直接调用一个或多个服务类处理。具体使用那一种模式,应根据你的项目情况来决定,这里不展开细说了。
& & 最后所有的测试代码可以在这里下载/jjyjjyjjy/TestAsp_Net_Mvc_ControllerActivator_DI.rar
已发表评论数()
&&登&&&陆&&
已收藏到推刊!
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见asp.net mvc4中关于model的一个疑义_Global中的Application_Start不走断点,高手分析一下原因,多谢_ios应用绑定微博账号,如何实现__脚本百事通
稍等,加载中……
^_^请注意,有可能下面的2篇文章才是您想要的内容:
asp.net mvc4中关于model的一个疑义
Global中的Application_Start不走断点,高手分析一下原因,多谢
ios应用绑定微博账号,如何实现
asp.net mvc4中关于model的一个疑义
asp.net mvc4中关于model的一个疑问http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/accessing-your-models-data-from-a-controller
原文在这里,我一步一步按照教程操作,
可是到了右键controllers文件夹 add controller的时候.
我的下拉框选项里没有教程上Movie这个模型.
麻烦知道的能告诉我一下么?在下不甚感激.
------解决方案--------------------vs的问题,你编译一下,再添加controller应该就会有了------解决方案--------------------是否 添加了数据库的模型映射 比如linq to entity
如果是自己定义的模型,你完全可以在页面 直接@model TestProgram.Models.User 来引入------解决方案--------------------先编译一下 模型所属项目.------解决方案--------------------重新编译下就出来。
另外Model要定义成public类型的哦。
Global中的Application_Start不走断点,高手分析一下原因,多谢
Global中的Application_Start不走断点,高手分析一下原因,谢谢Global中的Application_Start不走断点,高手分析一下可能的原因,先谢谢了我用的是vs2008------解决方案--------------------
没用过2008try附加进程到iis , 停掉你的asp.net 网站,再启动
------解决方案--------------------Application_Start僅在網站第一次被訪問時觸發。第一次調試時,可以在Application_Start中捕獲斷點,以後是捕獲不到的。除非你:1. 更改global.asax文件並保存,再調試.2. 將IIS或者內置的ASP.NET Development Server關閉,再調試。------解决方案--------------------更正下:2. 將IIS重啟或者將內置的ASP.NET Development Server關閉,再調試。
------解决方案--------------------我的执行。我用的是VS2008C# code
&%@ Application Language="C#" %&
&script runat="server"&
void Application_Start(object sender, EventArgs e)
void Application_End(object sender, EventArgs e)
在应用程序关闭时运行的代码
ios应用绑定微博账号,如何实现
ios应用绑定微博账号,怎么实现?我要实现一个ios应用绑定微博账号的功能,就是在我的应用中,不想注册的话,就直接点击一个按钮,就可以使用新浪微博或者是QQ号码直接授权登录,点击“立即绑定微博账号”的链接,弹出授权网站的页面,点“授权”按钮,则将当前登录的用户与其微博绑定,并跳回到之前的页面,哥哥姐姐们讲一下这个其中的原理吧,,请问我要怎么做才能实现这个功能呢?O(∩_∩)O先谢谢~~啦啦啦~~~
------解决方案--------------------oc调浏览器, 浏览器里面调微博登陆页面接口,然后接收浏览器的返回值保存都本地,------解决方案--------------------新浪的sso 已经实现了你说的这些功能,只要调用它的SSO SDK里的接口就可以实现了------解决方案--------------------可以看一下这个文档,里面比较详细的讲解了如何绑定的
/wiki/授权机制说明------解决方案--------------------仔细研究一下微博的官方文档
用他们提供的SDK比较方便
如果您想提高自己的技术水平,欢迎加入本站官方1号QQ群:&&,&&2号QQ群:,在群里结识技术精英和交流技术^_^
本站联系邮箱:Download ASP.NET MVC 4 for Visual Studio 2010 SP1 and Visual Web Developer 2010 SP1 from Official Microsoft Download CenterWarning: This site requires the use of scripts, which your browser does not currently allow.
ASP.NET MVC 4 for Visual Studio 2010 SP1 and Visual Web Developer 2010 SP1
Select Language:
Chinese (Simplified)Chinese (Traditional)CzechEnglishFrenchGermanItalianJapaneseKoreanPolishPortuguese (Brazil)RussianSpanishTurkish
ASP.NET MVC 4 provides a Model-View-Controller (MVC) framework for developing Web applications using Visual Studio 2010 SP1 or Visual Web Developer 2010 SP1.
4AspNetMVC4Setup.exe10/26/201235.9 MB
ASP.NET MVC 4 is a framework for developing highly testable and maintainable Web applications that follow the Model-View-Controller (MVC) pattern. The framework encourages you to maintain a clear separation of concerns— views for UI, controllers for handling user input, and models for domain logic. ASP.NET MVC applications are particularly suited for unit testing and using test-driven development (TDD) techniques. ASP.NET MVC 4 makes it easy to write applications for the mobile web through adaptive rendering and device specific display modes.
ASP.NET MVC 4 also includes:
o ASP.NET Web API, a framework for building and consuming HTTP services that can reach a broad range of clients including browsers, phones, and tablets. ASP.NET Web API is great for building services that follow the REST architectural style, plus it supports RPC patterns.
o ASP.NET Web Pages and the new Razor syntax provide a fast, approachable, and lightweight way to combine server code with HTML to create dynamic web content.
o Web Optimization, a framework for bundling and minifying scripts and CSS files.
o NuGet, a free, open source developer focused package management system for the .NET platform intent on simplifying the process of incorporating third party libraries into a .NET application during development.
Supported Operating System
Windows 7, Windows Server -Bit x86), Windows Server
editions, Windows Server 2003 Service Pack 2, Windows Server 2008, Windows Server 2008 R2, Windows Vista Service Pack 2, Windows XP Service Pack 3
PowerShell 2.0, .NET 4, ASP.NET 4, and Visual Studio 2010 SP1 or Visual Web Developer 2010 SP1 are required to use this feature.
For more information about installing this release, please see the .
The Microsoft .NET Framework 4 web installer package downloads and installs the .NET Framework components required to run on the target machine architecture and OS. An Internet connection is required during the installation. .NET Framework 4 is required to run and develop applications to target the .NET Framework 4.
The Visual C++ Redistributable Packages install run-time components that are required to run C++ applications that are built by using Visual Studio 2013.
.NET Framework 4.5 is a highly compatible, in-place update to .NET Framework 4.
The Microsoft Visual C++ 2010 Redistributable Package installs runtime components of Visual C++ Libraries required to run applications developed with Visual C++ on a computer that does not have Visual C++ 2010 installed.
The Visual C++ Redistributable Packages install runtime components that are required to run C++ applications built with Visual Studio 2012.
Loading your results, please wait...
Security patches
Software updatesService packsHardware driversEverything you need to create great apps.

我要回帖

更多关于 mvc4 ef 实现增删改查 的文章

 

随机推荐