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
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.
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.
I'm not sure what I did, and this just happened today.
I am unable to browse one of my MVC 4 applications, which I was previously able to do.
There's no error message.
When I right click on my project and view in browser, it just shows "Connecting..." - localhost:55223, and it remains there like forever.
I have tried the following:
1) Copied the proejct onto another PC and browse - works!
2) Create a new MVC application in visual studio and browse - works!
I have set a breakpoint in global.asax's start method, and it doesn't even get there.
What else do I have to check?
Please help
Have you tried right clicking on the IIS icon in you taskbar and exiting it? Then build the project again to restart IIS.
Sorry I cannot comment because i am unable to see comment button.
See your global.asax RegisterRoutes method is it fine? for example:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "GridPaging",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional}
);
}
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've setup an ASP.NET MVC 2 site several times on our test system on IIS 6. I'm fine with having to use the .aspx extension on controllers. The Global.asax.cs file looks like this:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.html/{*pathInfo}");
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"{controller}.aspx/{action}/{id}",
new { controller = "Account", action = "LogOn", id = UrlParameter.Optional }
);
routes.MapRoute(
"Root",
"",
new { controller = "Account", action = "LogOn", id = "" }
);
}
Other than this, deployment is pretty basic. I copied the files to the server, created a new virtual directory in IIS, set Account.aspx as the default document, and clicked ok.
Repeating these same steps for production doesn't work. It seems like IIS 6/ASP.NET doesn't want to route correctly (even thought it did so just fine on our test server).
My url looks like this:
http://_server_name:90/<IS APPLICATION NAME/Account
The site load with the basic IIS 'site cannot be found'. The url has been changed to look like:
http://_server_name>:90/IIS APPLICATION NAME/CustomErrorView?aspxerrorpath=/_APPLICATION_NAME_/Account.aspx/Logon
(underscores begin and end place holder values and are not literally in the url).
CustomErrorView is a view I created for custom errors to forward to (including 404's).
Both servers are running windows 2003.
Any thoughts?
Try making a Webform page that just redirect to your path!! And configure iis to point that page!!!! Let me know if was this scenario
Okay, I'm an idiot. When deploying an asp.net mvc application, it helps to ensure mvc is installed on the server. I installed it through the web platform installer and, presto, everything worked.
Sorry about such a lame question. I was moving to production and the steps I just knew should work didn't work and I sort of panicked.
Thanks for your input Marco. Haven't had a chance to try that solution, but its running now.