且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我可以使用ASP.NET MVC和PhoneGap编写移动应用程序吗?

更新时间:2023-02-15 22:32:35

只需构建一个移动设备友好的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 [ ^ ])你可以使用控制器用于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! :-)