WebForm-inductive MVC controller in. Net

Source: Internet
Author: User

In the current. NET comparison of fire pattern MVC mode, to tell the truth for the rookie I have not encountered a more robust MVC model of the project is also more regrettable. Occasionally on the Internet to see the WebForm implementation of MVC pattern (mainly controller ... Learned a wave, the following is how to achieve the introduction.

The small master uses the VS2010, first opens vs creates a new empty Web application:

After you create a new MVC web such as ...

After the project is newly completed, such as

Open the Web. config configuration file for MVC and WebForm separately (left: MVC, right: WebForm)

On the left <system.web> below

<compilation debug= "True" targetframework= "4.0" >
<assemblies>
<add assembly= "System.Web.Abstractions, version=4.0.0.0, culture=neutral, publickeytoken=31bf3856ad364e35"/>
<add assembly= "System.Web.Routing, version=4.0.0.0, culture=neutral, publickeytoken=31bf3856ad364e35"/>
<add assembly= "SYSTEM.WEB.MVC, version=2.0.0.0, culture=neutral, publickeytoken=31bf3856ad364e35"/>
</assemblies>
</compilation>

The configuration references three assemblies, first introducing System.Web.Abstractions, System.Web.Routing, SYSTEM.WEB.MVC assemblies in WebForm, and then adding the labels above. (Introducing an assembly, we can see the location of the assembly in the reference below MVC, and you can copy and paste it directly).

When the above operation is complete, we find that there is no Global.asax under WebForm, and we add a ' global application class ' such as:

Looking at the differences between MVC and WebForm's global, we found that there was a routing configuration in MVC. So we also like this configuration, we can create new MVC folder inside add RouteConfig.cs and Controllers folder

internal static void RegisterRoutes (RouteCollection routes)        {            routes. Ignoreroute ("{resource}.axd/{*pathinfo}");            Routes. MapRoute (                "Default",//route name                "{controller}/{action}/{id}",//URL with parameter                new {controller = "Home", action = "I Ndex ", id = urlparameter.optional}//parameter default value            );        }

Then configure the route in global:

  protected void Application_Start (object  sender, EventArgs e)        {            arearegistration.registerallareas ();            Routeconfig.registerroutes (routetable.routes);        }

Then in the Controllers folder, the new class inherits the controller (the new class must end with a controller)

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSYSTEM.WEB.MVC;namespacewebfrom.mvc.controllers{ Public classTestcontroller:controller { Public stringGetmsg (stringmsg) {            //var msg=request[""]            returnmsg+"Test Success"; }    }}

Finally, the new page is tested:

<Body>    <formID= "Form1"runat= "Server">    <Div>        <inputID= "Test"type= "button"value= "Test"onclick= "fngetmsg ()" />    </Div>    </form>    <Script>        functionfngetmsg () {varmsg= $("#test"). Val (); $.post (".. /.. /test/getmsg", {msg:msg},function(data) {if(data) alert (data);        }); }    </Script></Body>

Operation Result:

Done.....

  

WebForm-inductive MVC controller in. Net

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.