I have created an MVC project (Say DeploymentTool) and added another webAPI project to the solution (Say DeploymentToolAPI), and hence NameSpace is different for both. Now what I want is, to call any HTTPPOST/ HTTPGET method of DeploymentToolAPI from outside, say POSTMAN or web browser. Do we need to update WebApiConfig.cs for that?
If not, how can I find the URL of DeploymentToolAPI. I am able to call any method of DeploymentTool but it's not working in case of DeploymentToolAPI.
Would appreciate any help/ suggestions.
Right click on your WEB API project and go to Properties
Go on Web tab and you will see project URL textbox
This will tell you the base url for your web api project. (ie. http://localhost:8526)
Then go to WebApiConfig.cs to check URL route pattern.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace StandardWebApiTemplateProject
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
If you have Route Template like -
routeTemplate: "api/{controller}/{id}" then your url will be like
ie. http://localhost:8526/api/ControllerName/89
and if you include action name in route template then case will be like below
routeTemplate: "api/{controller}/{action}/{id}" then your url will be like
ie. http://localhost:8526/api/ControllerName/ActionName/89
Firstly you need to Set Multiple Startup Projects to make both your API service and the MVC web application start up simultaneously:
In Visual Studio right click your solution -> Properties -> Select "Multiple startup projects:" -> set the Action of both projects to Start:
Once this is done follow the instructions posted by #Ankush Jain or just copy the WEB API url from the browser window, because when you run the project after setting multiple startup projects you will see that both your web api service and the MVC web application will be opened in the browser in two different tabs.
Related
I am having a curious issue with one of my projects in development. The issue is with links to a certain URL "//localhost:62168/Images/Index". I have buttons linking to that URL but when "//localhost:62168/Images/" is accessed it returns a HTTP Error 403.14 - Forbidden error. See the below Image:
localhost//Images/
Oddly enough though, when I enter the URL exactly ("localhost:62168/Images/Index") it loads the page properly. See the below Image:
localhost/Images/Index
I've done plenty of research online and I believe it may be an issue with routing so below I've added the code of my "RouteConfig.cs" file. Unfortunately, I am new to ASP.net and MVC and not knowledgeable on proper Routing procedures:
using System.Web.Mvc;
using System.Web.Routing;
namespace ReedHampton
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}
I've also tried multiple other "solutions" presented in other online forums to no avail. These include:
Adding "directoryBrowse enabled="true" " to web.config
Running "C:\windows\Microsoft.NET\Framework64\v4.0.30319> aspnet_regiis.exe -i" in Command Prompt
Going through IIS Express and registering IIS Express and ASP.Net
I would greatly appreciate any help that anyone can provide!
IIS will attempt to short-circuit the request if it finds something on the filesystem that matches the URL. In other words, I'd assume you have an Images directory in your document root. Therefore, IIS will attempt to hit this directory, rather than pass the request on to the ASP.NET machinery. Since you've disabled directory browsing, you get a 403.
Long and short, you need to keep your ASP.NET MVC routes unique from what you have physically in your document root. You could change the name of the Images directory to something like img, or put it in a parent folder like the default of /Content/Images. Otherwise, you'll need to change your controller name or create a custom route to that controller not called /Images.
Currently i am using a web api for my asp.net application. I have put controller inside a folder called "Reports". The content of webApiConfig is following:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
When i try to access using this, the result shows "Not found". If i put the api diresctly in my project, it works.
I am a bit aware about areas in MVC, but as it is normal asp .net, i am not sure how to handle this problem.
Any help will br greatly appreciated.
It's working. I just needed to add Folder name between api & controller as below
api/<foldername>/{Controllername}/{actionmethod}/{id}
I am using following url now.
api/Reports/ReportController/LogReports
I am working on asp.net web api. I am trying to set the default route for my project in global.asax file like,
localhost:45678/api/Products
But i didnt find any format similar to asp.net mvc route model like
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
It always redirects me to Home page(HomeController). please guide me.
Check your your RouteConfig class in your App_Start folder. You should see the default route below which you can change.
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
EDIT
After reading your comments, I think the problem is not with your routes. I'm not sure why you would want to do it, but you just need to specify the Start URL for your project. Right click your web project - click Properties - click the Web tab - under Start Action select Start URL and enter http://localhost:45678/api/Products in the box. Save your project and run again. It should start at the new location.
The issue might be the common mistake that nearly lots of people get into.
The fact here is that the all routes get collected under System.Web.Routing.RouteTable.Routes collection regardless of the framework you use. So, if you put the ASP.NET MVC default route before the ASP.NET Web API route, the ASP.NET Web API route will never be scanned because the MVC route will be a match.
I am assuming this is the case here by looking at what you've provided so far. If that's not the case, upload the full solution out there somewhere and people can have a full look.
Actually if we are in the position to set the default route in properties->web->start location. then what is the need of route tables, custom routes,RegisterRoutes in global.asax file. I tried for this way
at first it seems like,
routes.MapHttpRoute(
name: "Default Api",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Now i want to make localhost:xxxx/api/products as default route for my web api then,
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/Products/{id}",
defaults: new { controller = "Products", id = RouteParameter.Optional }
);
But the results of no use.
While developing in Visiual Studio, you can set it by expand the Properties folder of the project. Then open launchSettings.json file and look for "launchUrl" property in this file
You can change the default launch route for the profile you are working on.
"launchUrl" : "api/products"
[EDIT: My answer is the same as Kevin's]
Are you saying that when you run the project from Visual Studio, it opens a browser to the project home page?
The Web API project template contains an MVC controller plus a Web API controller.
The URI "http://localhost:xxxx/" goes the MVC controller, while "http://localhost:xxx/api/products" goes to the API controller.
When you run the project in Visual Studio, it will navigate to "http://localhost:xxxx" by default. In normal operation, a client would request whichever URI it wanted.
You can change the Visual Studio settings under Project Properties / Web / Start Action.
I think you have danced around the solution but just missed the mark. Try this:
Make sure you are calling GlobalConfiguration.Configure(WebApiConfig.Register); in your Global.asax.cs.
In WebApiConfig.Register() Set the default route as:
routes.MapHttpRoute(name: "DefaultApi",
routeTemplate: "api/{Controller}/{id}",defaults: new { controller = "Products", id = RouteParameter.Optional });
In your web project settings make sure you have "Start Url" property set as:
localhost:45678
Good Luck.
In my ASP .NET Core Web API project, whenever you DEBUG the project, it fires up a browser to a predefined URL. This can be configered by right-clicking the project and going to properties and then Debug tab as shown below.
That said, not sure what the difference is between this and the launchsettings.json described in a previous answer above.
You need to add:
Response.Redirect("Products.aspx");
to method index() in the class HomeController.
I have a webforms application (VB.net, .NET 4.0) which is translated in three languages and I managed to localize everything except the URLs. My preference is to use routing, so I'd like to find a solution in this direction (not URL rewriting or IIS rewrites, etc.)
Out of many read articles I stumbled into this one as well: http://blog.maartenballiauw.be/post/2010/01/26/Translating-routes-%28ASPNET-MVC-and-Webforms%29.aspx, but this example is for MVC application. (As are the most of the questions here as well)
Any comments and ideas are very much welcome!
I could implement Maarten's solution in a WebForms ASP.NET application as described below.
I downloaded his sample and opened with Visual Studio. As you see it the essential part of translated routing can be found in Routing folder.
I created a new Class Library project in my web application, and copied these 5 files. This project missed some assembly references, so I added System.Web and System.Web.Routing to it.
After this there was only one problem in TranslatedRouteCollectionExtensions class. The two extension methods used MvcRouteHandler. This is the only piece of code which depends on MVC. To eliminate this dependency modify both extension methods like this:
public static TranslatedRoute MapTranslatedRoute(
this RouteCollection routes,
string name,
string url,
object defaults,
object routeValueTranslationProviders,
IRouteHandler routeHandler,
bool setDetectedCulture)
{
TranslatedRoute route = new TranslatedRoute(
url,
new RouteValueDictionary(defaults),
new RouteValueDictionary(routeValueTranslationProviders),
setDetectedCulture,
routeHandler);
routes.Add(name, route);
return route;
}
I added a reference in my Web Application to this Class Library project.
With this modification Maarten's example of register a translated route changes as follows:
routes.MapTranslatedRoute(
"TranslatedRoute",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" },
new { controller = translationProvider, action = translationProvider },
new GeneralRouteHandler(),
true
);
Implementing a custom route handler is not a complicated process. You can find many good articles about it.
We need a way to say
www.site.com/india
www.site.com/asia
www.site.com/usa
etc...
But we want all these requests to point back to www.site.com and not have to physically create the files and directories for every type of site... so if I just create a virtual directory (www.site.com/india) and point it to www.site.com... then I figure I can look at the URL and set some parameters/text/images accordingly to fill out the template...
Does this make sense? Or are there issues with web.configs ? OR a better way
I would encourage you to consider Routing, as demonstrated here: http://www.asp.net/mvc/tutorials/asp-net-mvc-routing-overview-cs
The given sample, repeated here so you can (hopefully) see the relevance, is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace MvcApplication1
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
}
}
By similar methods, you can merely extract the information you need
The first example was for MVC Routing, to do webforms routing you should look here http://msdn.microsoft.com/en-us/library/cc668202(VS.90).aspx and here's a (very limited) example:
public static void RegisterRoutes(RouteCollection routes)
{
routes.Add("BikeSaleRoute", new Route
(
"bikes/sale",
new CustomRouteHandler("~/Contoso/Products/Details.aspx")
));
}
Using a custom routehandler that they build in the code. Routehandlers are easy to use, once you get the hang of it. And they allow you more expressiveness.
I think you'll find that localizing the same site five ways without having different web.configs pointing you to your localization files will be difficult.
What I would do is have www.site.com's index wither automatically discover the client's location or ask them what version of the site they want. Then, refer them to a default.aspx under that site's virtual directory, that uses a common set of your custom web controls, localized according to the resource file referenced in that site's web.config. That way, all you'll need to copy is the site skeleton, which can be as little as the index.aspx.
If the site is mostly aspxs, this isn't going to work as well. In that case, I would do the website selection as before, but use a query string to keep track of client location and thus localization. That should still allow you to bring up the correct localization settings.
No, please promise me not to create a virtual directory for each country in the world. Checkout routing instead. And here are some other examples.
Consider using routing or even better MVC if you haven't written the sites already. You should definitely NOT be creating multiple virtual directories - it would be a nightmare to maintain