简体   繁体   中英

Creating a url in controller in asp.net mvc 4

I am trying to send activation mail to the currently registered user.In mail body,I need to send a link like http://example.com/account/activation?username=d&email=g.Now , for debugging on local machine, I manually write it as localhost:30995/account/activation?username=d&email=g. But, when my port number changes, I need to rewrite it. I tried another question on this website,but, compiler gives error like url.action doesnot exist.

Please give me fresh solution as I am confused with that solution.

Use a Url.Action overload that takes a protocol parameter to generate your URLs:

Url.Action("Activation", "Account", new { username = "d", email = "g" }, "http")

This generates an absolute URL rather than a relative one. The protocol can be either "http" or "https". So this will return http://localhost:XXXXX/account/activation?username=d&email=g on your local machine, and http://example.com/account/activation?username=d&email=g on production.

In short, this will stick whatever domain you're hosting your app on in front of your URL; you can then change your hostname/port number/domain name as many times as you want. Your links will always point to the host they originated from. That should solve the problem you're facing.

Try using IIS / IIS-Express instead of Casinni web server that comes with visual studio.

You could add bindings to have the right URL (with host entries of course).

This will avoid the port numbers in your links.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM