我可以使用ASP.NET MVC和PhoneGap编写移动应用程序吗? [英] Can I write a mobile app using ASP.NET MVC and PhoneGap?

查看:59
本文介绍了我可以使用ASP.NET MVC和PhoneGap编写移动应用程序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从一个简单的小型移动应用程序中快速获得一个演示。我已经决定使用PhoneGap打包它并使其适合,并使用普通的HTML 5和Angular来使其工作。



如果是app是静态的,如果需要,在设备上有本地数据存储,但我需要完整和正确的客户端 - 服务器。我最初的想法是最初开发应用程序作为ASP.NET MVC 5应用程序,具有漂亮的控制器和适当的数据库,并使用JSON与应用程序进行通信。在MVC中,HTML来自视图引擎,但是当准备好使用PhoneGap进行包装时,我将从所有控制器操作中捕获HTML并将其用于PhoneGap HTML源。



我咆哮完全错误的树,或者这是一个可行的方法,还有其他人尝试过这样的事吗?

解决方案

只需构建一个移动设备友好的MVC网站,人们可以在他们的手机上使用。如果您真的想要一个应用程序,您可以使用phonegap制作一个使用静态html \ _javascript的应用程序,该应用程序与您的适合移动设备的网站提供的服务进行通信。所以你最终会得到一个后端站点,为你的MVC网站前端和PhoneGap应用程序提供服务。


啊,这就是你需要一个Web API和一个客户!由于帖子中的大多数内容都是面向移动应用的。这意味着您希望将移动设备中的网站数据用作本机应用程序,而不是使用旧的默认浏览器加载HTML内容。我认为我说得对,就你的观点而言。



无论如何,ASP.NET MVC 5和MVC 6它们支持Web API,你可以通过客户端使用 HttpClient的。每种语言或框架都有它,您可以使用它们将请求发送到API以获取 JSON格式的响应。请注意,我还建议您使用JSON格式而不是XML,因为它的大小紧凑。 :-)



我从未使用PhoneGap构建任何应用程序(或任何声称构建 HTML,CSS和JavaScript中的本机应用程序的其他框架)但是由于他们声称,他们将为您提供C#或C ++等编程语言提供的所有工具。



第一阶段是创建API。在ASP.NET(MVC 5和MVC 6)中,您可以使用 ApiController [ ^ ]在MVC 5 for Web API 2和MVC 6中( ASP.NET 5 [< a href =https://msdn.microsoft.com/en-us/magazine/dn879354.aspxtarget =_ blanktitle =新窗口> ^ ])你可以使用控制器用于API并使用路由来配置用户可以从哪里调用它,例如 [Route(/ api / [controller])] 。其余的只是您使用的操作方法。



如果您有兴趣,我有一篇为ASP.NET 5的Web API编写的文章:在Windows 10本机应用程序中使用带有CRUD功能的ASP.NET 5 Web API [ ^ ]



至于客户端,您肯定可以在PhoneGap中构建应用程序,然后使用Google获取HttpClient和该框架中其他内容的结果。关于此讨论的社群主题之一是: http://community.phonegap.com/nitobi/topics/can_you_do_http_requests_with_phonegap [ ^ ]声称可以使用HTTP发送HTTP请求 XmlHttpRequest [ ^ ] JavaScript的对象。 这是非常期待的。 :叹气:



然后,您可以使用从API返回的数据在AngularJs中用于渲染目的。那么,这就是你要如何组成这个项目! : - )

I need to very quickly get a demo out of a small, simple mobile app. I have decided to use PhoneGap to package it and make it fit, and plain old HTML 5 and Angular to make it work.

That scenario is all very well if the app is static, with a local data store, if needed, on the device, but I need full and proper client-server. My initial thoughts are to initially develop the app as an ASP.NET MVC 5 app, with nice controllers and a proper database, and use JSON to communicate with the app. In MVC, the HTML comes from the view engine, but when ready for wrapping up with PhoneGap, I will capture the HTML from all controller actions and use that for the PhoneGap HTML source.

Am I barking up the totally wrong tree, or is this a feasible method, and has anyone else tried anything like this?

解决方案

Just build a mobile-friendly MVC site that people can use on their mobile. If you really want an "app" you can use phonegap to make an app that uses static html\javascript that communicates with the services provided by your mobile-friendly site. So you'll end up with one back-end site that services both your MVC website front end and also the PhoneGap app.


Ah, this is when you come to need a Web API and a client! Since most of your content in the post is mobile-app oriented. This means you want to use the data from your website in the mobile as a native application rather than HTML content being loaded using the old default browser. I think I got you right, on your point.

Anyways, ASP.NET MVC 5 and MVC 6 they support Web APIs that you can consume through clients using an HttpClient. Every language or framework has it, and you can use them to send the requests to the API to get response back in JSON format. Note that I would also recommend that you use JSON format instead of XML because of its compact size. :-)

I have never built any application using PhoneGap (or any other framework that claims to build "native applications in HTML, CSS and JavaScript") but since they claim, they will provide you with all the tools that a programming language such as C# or C++ provides.

First stage would be to create an API. In ASP.NET (MVC 5 and MVC 6) you can create the API, using ApiController[^] in MVC 5 for Web API 2 and in MVC 6 (ASP.NET 5[^]) you can use a Controller for API and use the routes to configure where user can call it from, such as [Route("/api/[controller]")]. The rest is just the action methods that you use.

I have an article written for ASP.NET 5's Web API, if you are interested: Consuming ASP.NET 5 Web API with CRUD functions in Windows 10 native application[^]

As for the client-side, you can surely built applications in PhoneGap and then use Google to get the results for HttpClient and other stuff in that framework. One of the community thread about this discussion is: http://community.phonegap.com/nitobi/topics/can_you_do_http_requests_with_phonegap[^] which claims that HTTP requests can be sent using the XmlHttpRequest[^] object of JavaScript. That was pretty much expected. :sigh:

You can then use the data returned from the API to be used in AngularJs for rendering purposes. So, that is how you are going to make up this project! :-)


这篇关于我可以使用ASP.NET MVC和PhoneGap编写移动应用程序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆